Skip to content

Commit a07c5ac

Browse files
author
Shuo
committed
Add: Decrypt String from Alphabet to Integer Mapping
1 parent 6aab5be commit a07c5ac

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package problem1309
2+
3+
func freqAlphabets(s string) string {
4+
l := len(s)
5+
ans, head := make([]byte, l), l-1
6+
for i := l - 1; i >= 0; i-- {
7+
if s[i] == '#' {
8+
ans[head] = 'a' + (s[i-1] - '1') + (s[i-2]-'0')*10
9+
i -= 2
10+
} else {
11+
ans[head] = 'a' + (s[i] - '1')
12+
}
13+
head--
14+
}
15+
return string(ans[head+1:])
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package problem1309
2+
3+
import "testing"
4+
5+
type testType struct {
6+
in string
7+
want string
8+
}
9+
10+
func TestFreqAlphabets(t *testing.T) {
11+
tests := [...]testType{
12+
{
13+
in: "10#11#12",
14+
want: "jkab",
15+
},
16+
{
17+
in: "1326#",
18+
want: "acz",
19+
},
20+
{
21+
in: "12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#",
22+
want: "abcdefghijklmnopqrstuvwxyz",
23+
},
24+
}
25+
for _, tt := range tests {
26+
got := freqAlphabets(tt.in)
27+
if got != tt.want {
28+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)