-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
Bug Report for https://neetcode.io/problems/string-encode-and-decode
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
While submitting a Go solution, the platform returned compilation errors such as newline in rune literal and other syntax errors. The same code compiled and ran correctly in my local Go environment.
After further testing, replacing the '$' delimiter in the code with '#' resolved the issue and the solution compiled successfully on the platform.
This suggests the platform may be preprocessing or interpreting $ characters before compilation, which alters the submitted Go source code.
Error Output:
# command-line-arguments
./main.go:22:22: newline in rune literal
./main.go:25:6: syntax error: unexpected name init, expected (
./main.go:36:22: syntax error: unexpected }, expected { after if clause
./main.go:42:6: syntax error: unexpected name main, expected (
./main.go:47:6: syntax error: unexpected name readInput, expected (
./main.go:184:2: syntax error: non-declaration statement outside function body
Failure code:
`type Solution struct{}
func (s *Solution) Encode(strs []string) string {
// as we just need to join the strings
// and the input contains all possible ascii character, cannot use generic values as joiner
// lets do a length-based encoding
encoded := ""
for _, str := range strs {
encoded += strconv.Itoa(len(str)) + "$" + str
}
return encoded
}
func (s *Solution) Decode(encoded string) []string {
decoded := []string{}
ind := 0
slen := 0
for ind < len(encoded) {
for i := ind; i < len(encoded); i++ {
if encoded[i] == '$' {
// get the length from the start
slen, _ = strconv.Atoi(string(encoded[ind:i]))
// move ind by i+1, we need to go to the actual string
ind = i + 1
break
}
}
// append from ind, to ind+slen to the end
decoded = append(decoded, encoded[ind:ind+slen])
ind = ind + slen
}
return decoded
}
`
Passing Case:
`type Solution struct{}
func (s *Solution) Encode(strs []string) string {
// as we just need to join the strings
// and the input contains all possible ascii character, cannot use generic values as joiner
// lets do a length-based encoding
encoded := ""
for _, str := range strs {
encoded += strconv.Itoa(len(str)) + "#" + str
}
return encoded
}
func (s *Solution) Decode(encoded string) []string {
decoded := []string{}
ind := 0
slen := 0
for ind < len(encoded) {
for i := ind; i < len(encoded); i++ {
if encoded[i] == '#' {
// get the length from the start
slen, _ = strconv.Atoi(string(encoded[ind:i]))
// move ind by i+1, we need to go to the actual string
ind = i + 1
break
}
}
// append from ind, to ind+slen to the end
decoded = append(decoded, encoded[ind:ind+slen])
ind = ind + slen
}
return decoded
}
`
Additional Notes:
- Code works correctly when run locally.
- Changing the delimiter from $ to # resolves the issue on the platform.