Preface
Flipped strings are considered common in string algorithms, and are used as written test questions by many companies. "Flip string verbatim" is a replica of the flip string, and it is also a previous interview question from Google. The original question is as follows:
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain leading or trailing spaces and the words are always separated by a single space. For example, Given s = "the sky is blue", return "blue is sky the". Could you do it in-place without allocating extra space?
In short: "the sky is blue"—>"blue is sky the"
Therefore, for this article, the algorithm to solve is:
Flip the string verbatim, for example: "the sky is blue"—>"blue is sky the"
Next, let’s take a look at the implementation ideas and code.
Implementation ideas and code
Since it is a string flip, we can use the previous idea of rewriting strings to solve it, but this question requires two flips:
The first flip, the overall flip: "the sky is blue" -> "eulb si yks eht"
The second flip, the word flip: "eulb si yks eht" -> "blue is sky the"
Therefore, first, an algorithm that can flip local and all strings can be implemented, passing in a character array, startIndex and endIndex, where startIndex and endIndex are the starting and end subscripts of the string to be flipped, that is, the characters between startIndex and endIndex are (included). The code is as follows:
func _reverseStr( _ chars:inout [Character], _ startIndex:Int, _ endIndex:Int){ var startIndex = startIndex var endIndex = endIndex if startIndex <= endIndex { let tempChar = chars[endIndex] chars[endIndex] = chars[startIndex] chars[startIndex] = tempChar startIndex += 1 endIndex -= 1 _reverseStr(&chars,startIndex,endIndex) } }
After that, you can use the above algorithm to complete the two flips mentioned above:
func reverseWords(_ str:String) -> String{ var chars = [Character]() //First flip all characters in the entire string, "the sky is blue" -> "eulb si yks eht" _reverseStr(&chars,0,-1) //Then flip the characters in each word, "eulb si yks eht" -> "blue is sky the" var startIndex = 0 for endIndex in 0 ..< { if endIndex == - 1 || chars[endIndex + 1] == " " { _reverseStr(&chars, startIndex, endIndex) startIndex = endIndex + 2 } } return String(chars) }
Complete algorithm code:
//Flip the characters in the specified rangefunc _reverseStr( _ chars:inout [Character], _ startIndex:Int, _ endIndex:Int){ var startIndex = startIndex var endIndex = endIndex if startIndex <= endIndex { let tempChar = chars[endIndex] chars[endIndex] = chars[startIndex] chars[startIndex] = tempChar startIndex += 1 endIndex -= 1 _reverseStr(&chars,startIndex,endIndex) } } //Flip the string verbatimfunc reverseWords(_ str:String) -> String{ var chars = [Character]() //First flip all characters in the entire string, "the sky is blue" -> "eulb si yks eht" _reverseStr(&chars,0,-1) //Then flip the characters in each word, "eulb si yks eht" -> "blue is sky the" var startIndex = 0 for endIndex in 0 ..< { if endIndex == - 1 || chars[endIndex + 1] == " " { _reverseStr(&chars, startIndex, endIndex) startIndex = endIndex + 2 } } return String(chars) } reverseWords("the sky is blue") //return "blue is sky the"
Summarize
The above is about the method of turning strings verbatim by Swift algorithm. I hope that the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.