Journey of LeetCode|DAY 7
Preface
This is a new day to start my string journey.
Learn something new and keep reviewing what I learnt before.
1. Reverse String
LeetCode Link: 344. Reverse String
Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that:
0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
Analysis and Solution
Double Pointer
LeetCode C++ as followings Double Pointer
class Solution {
public:
void reverseString(vector<char>& s) {
for (int i = 0, j = s.size() - 1; i < s.size()/2; i++, j--) {//define pointers in the right and left;they move to middle one by one;then swap the value.
swap(s[i],s[j]);//swap the value they point
}
}
};
2. Reverse String II
LeetCode Link: 541. Reverse String II
Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.
If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.
Analysis and Solution
2K Steps
LeetCode C++ as followings 2K Steps
class Solution {
public:
string reverseStr(string s, int k) {
for (int i = 0; i < s.size(); i += (2 * k)) {//define i,j index,traverse the string in 2K characters,which means i=i+(2K)
// 1. reverse the first k characters for every 2k characters counting from the start of the string.
// 2. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.
if (i + k <= s.size()) {//there are first K characters we can reverse.
reverse(s.begin() + i, s.begin() + i + k );//reverse function from i and i+k
} else {
// 3. If there are fewer than k characters left, reverse all of them
reverse(s.begin() + i, s.end());//reverse function from i to end
}
}
return s;//return the new s
}
};
Self-Made Function
LeetCode C++ as followings Self-Made Function
class Solution {
public:
void reverse(string& s, int start, int end) {//define a reverse function
for (int i = start, j = end; i < j; i++, j--) {//traverse to swap the value
swap(s[i], s[j]);//swap function
}
}
string reverseStr(string s, int k) {//reverseStr function
for (int i = 0; i < s.size(); i += (2 * k)) {//define i index to traverse string in 2K
// 1. reverse the first k characters for every 2k characters counting from the start of the string.
// 2. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.
if (i + k <= s.size()) {//reverse the first k characters
reverse(s, i, i + k - 1);//apply in self made function
continue;//continue traverse s
}
// 3. If there are fewer than k characters left, reverse all of them
reverse(s, i, s.size() - 1);//reverse from i to end
}
return s;
}
};
3. Reverse Words in a String
LeetCode Link: 151. Reverse Words in a String
Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
Analysis and Solution
Self-Made Function
LeetCode C++ as followings Self-Made Function
class Solution {
public:
void reverse(string& s, int start, int end){ //self-made reverse function;left included and right included:[]
for (int i = start, j = end; i < j; i++, j--) {
swap(s[i], s[j]);
}
}
void removeExtraSpaces(string& s) {//remove all the spaces and add space between the words
int slow = 0; //define pointer
for (int i = 0; i < s.size(); ++i) { //traverse string
if (s[i] != ' ') { //deal when meet not space;which means delete all spaces
if (slow != 0) s[slow++] = ' '; //add space between words; slow != 0indicates it isnot first word,which need to add space
while (i < s.size() && s[i] != ' ') {
s[slow++] = s[i++];//make up this word.it is over when meet space
}
}
}
s.resize(slow); //the size of slow is the size after removing spaces
}
string reverseWords(string s) {
removeExtraSpaces(s); //remove extra spaces,keep only one space between words and no spaces at the head or tail of string
reverse(s, 0, s.size() - 1);//apply self made function, reverse all the elements
int start = 0; //the index of start of first word must be 0 after remove extra spaces
for (int i = 0; i <= s.size(); ++i) {
if (i == s.size() || s[i] == ' ') { //it is over when meet space or tail
reverse(s, start, i - 1); //reverse word
start = i + 1; // updte the index of the next word
}
}
return s;
}
};
4. replace space
LeetCode Link: replace space
Please implement a function that replaces each space in the string s with “%20”
Analysis and Solution
Double Pointer
LeetCode C++ as followings Double Pointer
class Solution {
public:
string replaceSpace(string s) {
int count = 0; // count the quantity of spaces
int sOldSize = s.size();
for (int i = 0; i < s.size(); i++) {//traverse s
if (s[i] == ' ') {//meet the space
count++;//count +1
}
}
// extend the size of s; one space=%20
s.resize(s.size() + count * 2);
int sNewSize = s.size();
// replace space with %20 from last to first
for (int i = sNewSize - 1, j = sOldSize - 1; j < i; i--, j--) {//define the location of i j index
if (s[j] != ' ') {//new seat of element
s[i] = s[j];
} else {//replace space with %20
s[i] = '0';
s[i - 1] = '2';
s[i - 2] = '%';
i -= 2;//i=i-2
}
}
return s;
}
};
5. Rotate string left
LeetCode Link: Rotate string left
The left rotation operation of a string is to transfer several characters in front of the string to the end of the string. Please define a function to implement the function of left rotation of strings. For example, input the string “abcdefg” and the number 2, the function will return the result “cdefgab” obtained by left-rotating two places.
Analysis and Solution
Reverse
LeetCode C++ as followings Reverse
class Solution {
public:
string reverseLeftWords(string s, int n) {
reverse(s.begin(), s.begin() + n);//reverse string from start to n
reverse(s.begin() + n, s.end());//reverse string from n to end
reverse(s.begin(), s.end());//reverse the whole string
return s;
}
};