MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / Decode

Method Decode

encode_and_decode_strings_271/solution.go:24–49  ·  view source on GitHub ↗

Decodes a single string to a list of strings.

(strs string)

Source from the content-addressed store, hash-verified

22
23// Decodes a single string to a list of strings.
24func (codec *Codec) Decode(strs string) []string {
25 s := make([]string, 0)
26 for i := 0; i < len(strs); i++ {
27 var b bytes.Buffer
28
29 // read string header until an underscore is seen
30 charCnt := 0
31 for string(strs[i]) != "_" {
32 l, _ := strconv.Atoi(string(strs[i]))
33 charCnt = (charCnt + l) * 10
34 i++
35 }
36 charCnt /= 10
37
38 // add charCnt characters to the buffer
39 for j := 1; j <= charCnt; j++ {
40 b.WriteByte(strs[i+j])
41 }
42 s = append(s, b.String())
43
44 // move i forward over the last read string
45 i += charCnt
46 }
47
48 return s
49}

Callers 1

TestEncodeAndDecodeFunction · 0.95

Calls

no outgoing calls

Tested by 1

TestEncodeAndDecodeFunction · 0.76