The title
Reverse the word III in the string
Given a string, you need to reverse the character order of each word in the string, while still preserving the Spaces and the original order of the words.
Example:
Enter: "Let's take LeetCode contest" Output: "s 'tel ekat edoCteeL tsetnoc"Copy the code
Tip:
- In a string, each word is separated by a single space, and there are no extra Spaces in the string.
A string b string C string D string
Analysis of the
According to the question, each word is divided by a single space, so I think of the three big steps shown in the picture above:
- use
split()
Method to press the stringThe blank spaceDivide and form an array - We’re going to do each element of the array, and we’re going to invert the string for each element
- use
jion()
Method to use the processed arrayThe blank spaceConcatenated converts back to a string
The first step and the third step are easier to deal with, the difficulty in the second step.
Step 1: Turn strings into arrays
The first step uses the split(“) method to split the string into arrays by Spaces. Note: single quotes have a space between them!!
The segmented data structure is shown below
[
"Let's"."take"."LeetCode"."contest"
]
Copy the code
Step 2: Reverse operation (difficult point)
The second step is to reverse the character order of the array elements, but not the order of the array elements.
It occurred to me that arrays have a reverse related method: reverse().
However, reverse() is used to reverse the order of array elements rather than strings, so we can use the same idea as in the first step, and convert each item of the array into an array using the split() method, which is a two-dimensional array with the structure shown below
[["L"."e"."t"."'"."s"],
["t"."a"."k"."e"],
["L"."e"."e"."t"."C"."o"."d"."e"],
["c"."o"."n"."t"."e"."s"."t"]]Copy the code
Then use the reverse() method to reverse the data in the second dimension, and the data structure looks like this
[["s"."'"."t"."e"."L"],
["e"."k"."a"."t"],
["e"."d"."o"."C"."t"."e"."e"."L"],
["t"."s"."e"."t"."n"."o"."c"]]Copy the code
Step 3: Array back to string
After the second step, the data becomes a two-dimensional array. At this point, you only need to convert the two-dimensional array into a one-dimensional array, and then the one-dimensional array into a string.
2d to 1D: join(”). Notice there is no space
[
"s'teL"."ekat"."edoCteeL"."tsetnoc"
]
Copy the code
One-dimensional conversion string: join(‘ ‘). There is a space at this time!!
"s'teL ekat edoCteeL tsetnoc"
Copy the code
coding
Along the lines above, the code looks like this
let str = "Let's take LeetCode contest"
function reverseWords(str) {
// [Step 1] The string is separated by Spaces to save the array. The order of the elements in the array is the order of the words
let arr = str.split(' ')
// [Step 2] Iterate through the array, inverting each element
let result = arr.map(item= > {
return item.split(' ').reverse().join(' ')})// [step 3] Convert the array to a string separated by Spaces
return result.join(' ')}console.log(reverseWords(str)) // "s'teL ekat edoCteeL tsetnoc"
Copy the code
To optimize the
[Option 1] Reduce variables
Follow the code above to reduce the declaration of unnecessary variables
let reverseWords = function(str) {
return str.split(' ').map(item= > {
return item.split(' ').reverse().join(' ')
}).join(' ')}Copy the code
[Scheme 2] Regular match 1
let reverseWords = function(str) {
// re \s means space
return str.split(/\s/g).map(item= > {
return item.split(' ').reverse().join(' ')
}).join(' ')}Copy the code
[Scheme 3] Regular match 2
let reverseWords = function(str) {
// Identify the words one by one \w denotes the letter 'denotes'.
return str.match(/[\w']+/g).map(item= > {
return item.split(' ').reverse().join(' ')
}).join(' ')}Copy the code
[Option 4] A line of code
let reverseWords = function(str) {
return str.split(' ').reverse().join(' ').split(/\s/g).reverse().join(' ')}Copy the code
[Scheme 5] Expand operator
let reverseWords = str= > [...str].reverse().join(' ').split(' ').reverse().join(' ')
Copy the code
In fact, these methods are basically used to solve the same idea.