Reverse (Words in a) String
Problem 1 : reverse a string
Solution: we can use the inner-built function of StringBuilder to reverse a string or just switch the start and end step by step.
Code:
public class Solution { public String reverseString(String s) { StringBuilder sb = new StringBuilder(s); return sb.reverse().toString(); } }
Problem 2: Reverse Vowels of a String
Solution:
1. We need to know which letters are vowels : a o e u i A O E U I
2. Use a hashset to store these letters so that we can determine if a letter is vowel in O(1), and use a char array instead of string.
3. Use two pointers, start which point to the next vowel from the beginning and end which point to the next vowel from the end
4. Use while loop to find the vowel of both sides and make sure that start < end
5. Every time we find the vowel of the two sides, swap them.
6. Build the result string with the final char array -> new String(chararray);
Code:
public class Solution { public String reverseVowels(String s) { Set<Character> vowels = new HashSet<>(); vowels.add('a'); vowels.add('o'); vowels.add('e'); vowels.add('i'); vowels.add('u'); vowels.add('A'); vowels.add('O'); vowels.add('E'); vowels.add('I'); vowels.add('U'); char[] s_array = s.toCharArray(); int start = 0, end = s.length()-1; while(start<end){ while(start<end && !vowels.contains(s_array[start])) start++; while(end>start && !vowels.contains(s_array[end])) end--; if(start!=end){ s_array[start]^=s_array[end]; s_array[end]^=s_array[start]; s_array[start++]^=s_array[end--]; } } return new String(s_array); } }
Problem 3: Reverse String II
Given a string and an Integer k, reverse the first k characters for every 2k characters. If there are less than k characters, reverse all of them. If there are more than k and less than 2k characters, reverse the first k characters.
e.g: abcdefghij k=3 -> cbadefihgj
Solution:
1. Transform the string to char array. Use a for loop, i+=2k, every iteration, determine the end point of the substring that need to be reversed->i+k-1 and if it is beyond the length of the string, just set the end as the final character of the string.
2. reverse the substring indexed from i to end.
3. build the new string and return it.
Code:
public class Solution { public String reverseStr(String s, int k) { char[] arr = s.toCharArray(); for(int i = 0; i < s.length(); i+=2*k){ int end = (i+k>s.length())?(s.length()-1):(i+k-1); reverse(arr, i, end); } return new String(arr); } private void reverse(char[] arr, int start, int end){ while(start<end){ arr[start]^=arr[end]; arr[end]^=arr[start]; arr[start++]^=arr[end--]; } } }
Problem 4: Reverse Words in a String
Words are separated using space and we want to reverse every word and return the new string.
Solution:
1. transform the string into char array, use a variable start to record the start point that need to reverse.
2. use a for loop and each time we encounter a space at index i, reverse from start to i-1 and set start to i+1
3. after the loop if start is still less than length-1, which means the start point is not the final character, we reverse from start to the final character of the string.
4. build the new string with char array and return it.
Code:
public class Solution { public String reverseWords(String s) { if(s.length()<2) return s; char[] arr = s.toCharArray(); int start = 0; for(int i = 0; i < arr.length; i++){ if(arr[i]==' '){ reverse(arr, start, i-1); start = i+1; } } if(start < arr.length-1) reverse(arr, start, arr.length-1); return new String(arr); } private void reverse(char[] arr, int start, int end){ while(start<end){ arr[start]^=arr[end]; arr[end]^=arr[start]; arr[start++]^=arr[end--]; } } }

浙公网安备 33010602011771号