File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ package kotlin
2
+
3
+ class Codec {
4
+ // Encodes a list of strings to a single string.
5
+ fun encode (strs : List <String >): String {
6
+ val stringBuilder = StringBuilder ()
7
+ for (string in strs) {
8
+ for (char in string) {
9
+ stringBuilder.append(char.toInt()) // char.code in newer version's of Kotlin
10
+ stringBuilder.append(CHAR_DELIMITER )
11
+ }
12
+ stringBuilder.append(STRING_DELIMITER )
13
+ }
14
+ return stringBuilder.toString()
15
+ }
16
+
17
+ // Decodes a single string to a list of strings.
18
+ fun decode (s : String ): List <String > {
19
+ val stringBuilder = StringBuilder ()
20
+ val resultantList = mutableListOf<String >()
21
+ var i = 0
22
+ while (i in s.indices) {
23
+ while (s[i] != STRING_DELIMITER ) {
24
+ var charIntegerValue = " "
25
+ while (s[i] != CHAR_DELIMITER ) {
26
+ charIntegerValue + = s[i]
27
+ i++
28
+ }
29
+ stringBuilder.append(charIntegerValue.toInt().toChar())
30
+ i++
31
+ }
32
+ resultantList.add(stringBuilder.toString())
33
+ stringBuilder.clear()
34
+ i++
35
+ }
36
+ return resultantList
37
+ }
38
+
39
+ companion object {
40
+ private const val CHAR_DELIMITER = ' |'
41
+ private const val STRING_DELIMITER = ' /'
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments