Skip to content

Commit 678b7f3

Browse files
authored
neetcode와 같은 솔루션
1 parent 04fe403 commit 678b7f3

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+
type Codec struct {
2+
}
3+
4+
// Encodes a list of strings to a single string.
5+
func (codec *Codec) Encode(strs []string) string {
6+
var temp []rune
7+
for _, v := range strs {
8+
var current_string = []rune(v)
9+
temp = append(temp, rune(len(current_string)))
10+
temp = append(temp, rune('π'))
11+
temp = append(temp, current_string...)
12+
}
13+
return string(temp)
14+
}
15+
16+
// Decodes a single string to a list of strings.
17+
func (codec *Codec) Decode(strs string) []string {
18+
var full_string = []rune(strs)
19+
var result []string
20+
21+
var index = 0
22+
23+
for index < len(full_string) {
24+
var word_start int = int(index)
25+
for full_string[word_start] != rune('π') {
26+
word_start += 1
27+
}
28+
length := int(full_string[word_start-1])
29+
result = append(result, string(full_string[word_start+1:word_start+1+length]))
30+
index = word_start + 1 + length
31+
}
32+
return result
33+
}

0 commit comments

Comments
 (0)