-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlify.java
More file actions
55 lines (51 loc) · 1.75 KB
/
urlify.java
File metadata and controls
55 lines (51 loc) · 1.75 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
47
48
49
50
51
52
53
54
55
/**
* urlify
*/
/*
* Overall concept
* We are given a character array that is guartuened to have enough space so
* that we can do everything inplace
* We are given the true length so we know when the actual string ends.
* By using this length + the number of (spaces * 2) (each space in the string
* will add 2 new characters) we can compute the
* length of the actual array to find the end (we could also do str.length - 1
* but this way is cooler)
* Now we itterate backwards and essentially copy over the array that way. If we
* see a space we add %20 to the back and otherwise
* we just copy the character.
*
* Question: Why is there no issue with overriding characters when replacing
* spaces?
*
* Answer: Let's assume that we overrode some character so now we are at some
* position n. And we assume character we overrode,
* for the sake of simplicity, was at n - 1 (could be b4 but doesn't matter).
* Now once we reach the end of the array, index
* would be at 0 while i would be = 1. This contradicts the fact that the array
* is long enough to hold the entire new string
* and thus does not need to be worried about
*
*/
public class urlify {
public static void url(char[] str, int trueLen) {
int spaceCount = 0;
for (int i = 0; i < trueLen; i++) {
if (str[i] == ' ') {
spaceCount++;
}
}
int index = trueLen + spaceCount * 2;
index--;
for (int i = trueLen - 1; i >= 0; i--) {
if (str[i] == ' ') {
str[index] = 0;
str[index - 1] = 2;
str[index - 2] = '%';
index -= 3;
} else {
str[index] = str[i];
index--;
}
}
}
}