File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments