MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / RLEncode

Function RLEncode

compression/rlecoding.go:22–34  ·  view source on GitHub ↗

RLEncode takes a string and returns its run-length encoding

(data string)

Source from the content-addressed store, hash-verified

20
21// RLEncode takes a string and returns its run-length encoding
22func RLEncode(data string) string {
23 var result string
24 count := 1
25 for i := 0; i < len(data); i++ {
26 if i+1 < len(data) && data[i] == data[i+1] {
27 count++
28 continue
29 }
30 result += fmt.Sprintf("%d%c", count, data[i])
31 count = 1
32 }
33 return result
34}
35
36// RLEdecode takes a run-length encoded string and returns the original string
37func RLEdecode(data string) string {

Callers 2

TestCompressionRLEncodeFunction · 0.92
BenchmarkRLEncodeFunction · 0.92

Calls

no outgoing calls

Tested by 2

TestCompressionRLEncodeFunction · 0.74
BenchmarkRLEncodeFunction · 0.74