Title description: Please implement a function to replace each space in the string s with "%20"
Example 1:
Input: s = "We are happy."
Output: "We%20are%20happy."
limit:
0 <= length of s <= 10000
Problem-solving ideas: double pointer traversal replacement method
class Solution {
public:
string replaceSpace(string s) {
int count = 0;
int len = s.size();
for(char c : s)
{
if( c==' ')
count++;
}
s.resize(len+count*2);
for(int i = len-1,j = s.size()-1;i < j;i--,j--)
{
if(s[i] != ' ')
{
s[j] = s[i];
}
else
{
s[j-2] = '%';
s[j-1] = '2';
s[j] = '0';
j -= 2;
}
}
return s;
}
};