-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffer05substitue_space.java
More file actions
48 lines (40 loc) · 1.06 KB
/
offer05substitue_space.java
File metadata and controls
48 lines (40 loc) · 1.06 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public String replaceSpace(String s) {
int space = 0;
for(int i = 0; i<s.length(); i++){
if(s.charAt(i) == ' ' ){
space++;
}
}
if(space==0) return s;
char[] c = new char[s.length()+2*space];
int j = 0;
for(int i = 0; i<s.length(); i++){
if(s.charAt(i) != ' ' ){
c[j] = s.charAt(i);
j++;
}else{
c[j++] = '%';
c[j++] = '2';
c[j++] = '0';
}
}
s = String.valueOf(c);
return s;
}
}
//string buffer
class Solution {
public String replaceSpace(String s) {
StringBuffer temp = new StringBuffer(10); //StringBuilder res = new StringBuilder("");(faster)
for(int i = 0 ;i < s.length(); i++ ){
if(s.charAt(i) ==' '){
temp.append("%20");
}
else{
temp.append(s.charAt(i));
}
}
return temp.toString();
}
}