Given a string s, replace all the spaces in the string with '%20'.
Examples:
Input: s = "i love programming"
Output: i%20love%20programming
Explanation: The 2 spaces are replaced by '%20'Input: s = "ab cd"
Output: ab%20cd
Try It Yourself
[Approach] Traverse the String - O(n) Time and O(n) Space
The idea is to iterate through each character of the input string and build a new string (or result container). Whenever a space character is encountered, replace it with the string "%20", otherwise append the current character as it is.
Dry run for s = "g ee ks":
- For i = 0, 'g' is not a space, so it is appended to result, making it "g".
- For i = 1, ' ' is a space, so "%20" is appended, making result = "g%20".
- For i = 2, 'e' is not a space, so it is appended, making result = "g%20e".
- For i = 3, 'e' is not a space, so it is appended, making result = "g%20ee".
- For i = 4, ' ' is a space, so "%20" is appended, making result = "g%20ee%20".
- For i = 5, 'k' is not a space, so it is appended, making result = "g%20ee%20k".
- For i = 6, 's' is not a space, so it is appended, making result = "g%20ee%20ks".
Traversal is complete, so the final result "g%20ee%20ks" is returned.
#include <iostream>
using namespace std;
string URLify(string& s) {
string result = "";
for (int i = 0; i < s.length(); i++) {
// if " " is encountered,
// append "%20" to string
if (s[i] == ' ') {
result += "%20";
}
// else append the current
// character.
else {
result += s[i];
}
}
return result;
}
int main() {
string s = "i love programming";
cout << URLify(s);
return 0;
}
class GfG {
static String URLify(String s) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
// if " " is encountered,
// append "%20" to string
if (s.charAt(i) ==' ') {
result.append("%");
result.append("2");
result.append("0");
}
// else append the current
// character.
else {
result.append(s.charAt(i));
}
}
return result.toString();
}
public static void main(String[] args) {
String s = "i love programming";
System.out.println(URLify(s));
}
}
def URLify(s) :
n = len(s)
# Use a list to mimic
# push_back behavior efficiently
result = []
for i in range(n):
# if " " is encountered,
# append "%20" to the list
if s[i] == ' ':
result.append('%')
result.append('2')
result.append('0')
# else append the current
# character.
else:
result.append(s[i])
# Join the list into a single string at the end
return "".join(result)
if __name__ == "__main__":
s = "i love programming"
print(URLify(s))
using System;
using System.Text;
public class GfG
{
static string URLify(string s) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.Length; i++) {
// if " " is encountered,
// append "%20" to string
if (s[i] == ' ')
result.Append("%20");
// else append the current
// character.
else
result.Append(s[i]);
}
return result.ToString();
}
public static void Main()
{
string s = "i love programming";
Console.WriteLine(URLify(s));
}
}
function URLify(s) {
let result = [];
for (let i = 0; i < s.length; i++) {
// if " " is encountered,
// append "%20" to string
if (s[i] === ' ') {
result.push('%', '2', '0');
} else {
result.push(s[i]);
}
}
return result.join('');
}
// Driver Code
let s = 'i love programming';
console.log(URLify(s));
Output
i%20love%20programming