The order of brushing is from: Code Casual Recording
strrev
344. Reverse string
Write a function that reverses the input string. The input string is given as an array of characters s.
Instead of allocating extra space to other arrays, you must modify the input arrays in place and use the extra space of O(1) to solve this problem.
Example 1:
Input: s = ["h","e","l","l","o"] Output:["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"] Output:["h","a","n","n","a","H"]
public void reverseString(char[] s) { if(s.length <= 1) return; int left = 0, right = s.length - 1; while(left < right) { char ch = s[left]; s[left] = s[right]; s[right] = ch; left++; right--; } }
541. Invert String II
Given a string s and an integer k, the first k characters of the 2k characters are inverted for every 2k character counted from the beginning of the string.
- If the remaining characters are less than k, invert all remaining characters.
- If the remaining characters are less than 2k but greater than or equal to k, the first k characters are inverted and the remaining characters remain unchanged.
Example 1:
Input: s = "abcdefg", k = 2 Output:"bacdfeg"
Example 2:
Input: s = "abcd", k = 2 Output:"bacd"
public String reverseStr(String s, int k) { char[] chars = s.toCharArray(); int left = 0, right = 0; while(left < chars.length - 1) { while(right < chars.length - 1 && right - left + 1 < 2 * k) { right++; } if(right - left + 1 == 2 * k) { resverse(chars, left, (left + right) / 2); } else { // Reverse the first k or all remaining characters resverse(chars, left, Math.min(right, left + k - 1)); } left = ++right; } return new String(chars); } // Reverse characters between chars'index left and right public void resverse(char[] chars, int left, int right) { while(left < right) { char ch = chars[left]; chars[left] = chars[right]; chars[right] = ch; left++; right--; } }
Better way: walk 2*k lattices each time, and judge the inverted right index value in the loop.
public String reverseStr(String s, int k) { char[] chars = s.toCharArray(); for(int i = 0; i < chars.length; i += 2 * k) { int end = Math.min(i + k - 1, chars.length - 1); resverse(chars, i, end); } return new String(chars); }
Replace spaces
Swordfinger Offer 05. Replace spaces
Please implement a function that replaces each space in string s with'%20'.
Example 1:
Input: s = "We are happy." Output:"We%20are%20happy."
public String replaceSpace(String s) { // Get the number of spaces in the string int count = 0; for(int i = 0; i < s.length(); i++) { if(s.charAt(i) == ' ') { count++; } } // Expanded Character Array char[] chars = new char[s.length() + 2 * count]; int pos = 0; // Copy characters from the original string to the expanded character array for(int i = 0; i < s.length(); i++) { if(s.charAt(i) == ' ') { chars[pos] = '%'; chars[++pos] = '2'; chars[++pos] = '0'; } else { chars[pos] = s.charAt(i); } pos++; } return new String(chars); }
Flip Words
151. Flip words in a string
Give you a string s, flipping all the words in the string one by one.
- The input string s can contain extra spaces before, after, or between words.
- After flipping, words should be separated by only one space.
- Flipped strings should not contain extra spaces.
Example 1:
Input: s = " hello world " Output:"world hello" Interpretation: Input strings can contain extra spaces before or after, but flipped characters cannot.
Example 2:
Input: s = "a good example" Output:"example good a" Interpretation: If there is extra space between two words, reduce the space between flipped words to only one.
The double pointer moves from right to left, with a word between left and right.
public String reverseWords(String s) { s = s.trim(); // Remove header and tail spaces StringBuilder res = new StringBuilder(); int left = s.length() - 1, right = s.length() - 1; // left, right is a word between for(int i = s.length() - 1; i >= 0; i--) { if(s.charAt(i) == ' ') { if(left == right) { // Encountered multiple spaces, left and right at the same time, skip spaces left--; right--; } else { // The first space encountered indicates that a word has been found res.append(s.substring(left + 1, right + 1) + " "); right = --left; } } else { // Look left until you find a complete word left--; } } // Add the last word if(left != right) { res.append(s.substring(left + 1, right + 1)); } return res.toString(); }
Rotate Character Left
Swordfinger Offer 58 - II. Left Rotating String
The left rotation of a string transfers several characters from the front to the end of the string. Please define a function to implement the string left rotation operation. For example, enter the string "abcdefg" and the number 2, which returns the result "cdefgab" by rotating two left places.
Example 1:
input: s = "abcdefg", k = 2 output: "cdefgab"
Example 2:
input: s = "lrloseumgh", k = 6 output: "umghlrlose"
Using substring, this method requests additional space for O(n).
public String reverseLeftWords(String s, int n) { StringBuilder res = new StringBuilder(); res.append(s.substring(n, s.length())); res.append(s.substring(0, n)); return res.toString(); }
Reverse the first n characters, then the characters after n, and finally the entire string. There is no additional space to request.
public String reverseLeftWords(String s, int n) { StringBuilder res = new StringBuilder(s); reverse(res, 0, n); reverse(res, n, s.length()); reverse(res, 0, s.length()); return res.toString(); } // left, right is left closed right open public void reverse(StringBuilder builder, int left, int right) { while(left < right) { char ch = builder.charAt(left); builder.setCharAt(left, builder.charAt(right-1)); builder.setCharAt(right-1, ch); left++; right--; } }
Implement strStr(): Classic KMP algorithm
28. Implement strStr()
Give you two strings, haystack and needle, and find the first place in the haystack string where the needle string appears (subscript starts at 0). If it does not exist, it returns -1.
For this topic, we should return 0 when needle is an empty string.
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba" Output:-1