-
Notifications
You must be signed in to change notification settings - Fork 1
/
46-Compress-Decompress(Ripple).go
105 lines (88 loc) · 2.51 KB
/
46-Compress-Decompress(Ripple).go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Objective: Write an algorithm to encode and decode a given string by
using the count of repeated characters (you can think of this as a simple
compression algorithm).
Example:
encode("AAABB") = "A3B2"
decode("A3B2") = "AAABB"
You need to implement two functions for a simple compression algorithm:
Encode: Compress a given string by using the count of repeated characters.
Decode: Decompress the string back to its original form.
Example:
Encoding: "AAABB" should be encoded as "A3B2".
Decoding: "A3B2" should be decoded back to "AAABB".
*/
/*
Edge Case - to Discuss
encoding needs to have edge case -where compression input should not increase the size.
Ex. AB should not give A1B1
*/
package main
import (
"fmt"
"strconv"
)
// encode compresses the string unless it would make the string longer.
func encode(input string) string {
if len(input) == 0 {
return ""
}
result := ""
for i := 0; i < len(input); i++ {
count := 1
// Count occurrences of the same character
for i+1 < len(input) && input[i] == input[i+1] {
i++
count++
}
// Append the character and count, only if count > 1
if count > 1 {
result += string(input[i]) + strconv.Itoa(count)
} else {
result += string(input[i])
}
}
return result
}
// decode expands the encoded string back to its original format.
func decode(input string) string {
result := ""
var numStr string
for i := 0; i < len(input); i++ {
char := input[i]
if char >= '0' && char <= '9' {
numStr += string(char)
} else {
if numStr != "" {
count, _ := strconv.Atoi(numStr)
result += repeatLastChar(result, count)
numStr = ""
}
result += string(char)
}
}
// Handle the last number if it exists
if numStr != "" {
count, _ := strconv.Atoi(numStr)
result += repeatLastChar(result, count)
}
return result
}
// repeatLastChar repeats the last character of a string count times.
func repeatLastChar(s string, count int) string {
if len(s) == 0 {
return ""
}
lastChar := s[len(s)-1]
result := ""
for i := 0; i < count; i++ {
result += string(lastChar)
}
return result
}
func main() {
original := "AB"
encoded := encode(original)
decoded := decode(encoded)
fmt.Printf("Original: %s, Encoded: %s, Decoded: %s\n", original, encoded, decoded)
}