forked from yanglr/leetcode-ac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (26 loc) · 739 Bytes
/
Solution.java
File metadata and controls
31 lines (26 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// import java.io.*;
class Solution {
public String replaceSpaces(String S, int length) {
if(length > S.length()) return "";
S = S.substring(0, length);
StringBuilder res = new StringBuilder();
for(int i=0; i < length; i++)
{
char ch = S.charAt(i);
if(ch != ' ')
res.append(ch);
else {
res.append("%20");
}
}
return res.toString();
}
// Test
public static void main(String[] args) {
Solution sol = new Solution();
String S = "Mr John Smith ";
int length = 14;
String res = sol.replaceSpaces(S, length);
System.out.println(res);
}
}