forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjaejeong1.java
More file actions
45 lines (39 loc) · 1.34 KB
/
jaejeong1.java
File metadata and controls
45 lines (39 loc) · 1.34 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
import java.util.ArrayList;
import java.util.List;
class SolutionEncodeAndDecodeStrings {
private static final char SEPERATOR = '/';
/*
* @param strs: a list of strings
* @return: encodes a list of strings to a single string.
*/
// 풀이: 문자열 길이를 구분자와 함께 encode해 decode시 문자열 길이를 참고할 수 있도록 한다
// 시간복잡도: O(N), 공간복잡도: O(1)
public String encode(List<String> strs) {
// write your code here
var answer = new StringBuilder();
for (var str : strs) {
answer.append(SEPERATOR)
.append(str.length())
.append(str);
}
return answer.toString();
}
/*
* @param str: A string
* @return: decodes a single string to a list of strings
*/
// 풀이: 문자열 길이를 구분자와 함께 encode해 decode시 문자열 길이를 참고할 수 있도록 한다
// 시간복잡도: O(N), 공간복잡도: O(N)
public List<String> decode(String str) {
// write your code here
List<String> answer = new ArrayList<>();
var i = 0;
while (i < str.length()) {
var seperatorIdx = str.indexOf(SEPERATOR, i) + 1;
var size = Integer.parseInt(str.substring(seperatorIdx, seperatorIdx + 1));
i = seperatorIdx + size + 1;
answer.add(str.substring(seperatorIdx + 1, i));
}
return answer;
}
}