Skip to content

Commit b02e135

Browse files
author
이연수
committed
encode and decode strings
1 parent d184bef commit b02e135

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode_study
2+
3+
/*
4+
* 문자열 인코딩 디코딩 문제
5+
* 시간 복잡도: O(n)
6+
* 공간 복잡도: O(n)
7+
* */
8+
fun encoding(strs: List<String>): String {
9+
var result = ""
10+
if (strs.isEmpty()) return result
11+
for (index in 0 until strs.size - 1) {
12+
if (strs[index] == ":") {
13+
result += strs[index] + "::;"
14+
} else {
15+
result += strs[index] + ":;"
16+
}
17+
}
18+
result += strs[strs.size - 1]
19+
return result
20+
}
21+
22+
fun decoding(str: String): List<String>{
23+
val splitedStrList = str.split(":;")
24+
val result = mutableListOf<String>()
25+
for (splitStr in splitedStrList){
26+
if (splitStr == "::") {
27+
result.add(":")
28+
} else {
29+
result.add(splitStr)
30+
}
31+
}
32+
return result
33+
}

0 commit comments

Comments
 (0)