-
Notifications
You must be signed in to change notification settings - Fork 1
/
reverse-only-letters.go
118 lines (99 loc) · 1.45 KB
/
reverse-only-letters.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
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
)
func reverseOnlyLetters(s string) string {
m := map[string]bool{
"A": true,
"B": true,
"C": true,
"D": true,
"E": true,
"F": true,
"G": true,
"H": true,
"I": true,
"J": true,
"K": true,
"L": true,
"M": true,
"N": true,
"O": true,
"P": true,
"Q": true,
"R": true,
"S": true,
"T": true,
"U": true,
"V": true,
"W": true,
"X": true,
"Y": true,
"Z": true,
"a": true,
"b": true,
"c": true,
"d": true,
"e": true,
"f": true,
"g": true,
"h": true,
"i": true,
"j": true,
"k": true,
"l": true,
"m": true,
"n": true,
"o": true,
"p": true,
"q": true,
"r": true,
"s": true,
"t": true,
"u": true,
"v": true,
"w": true,
"x": true,
"y": true,
"z": true,
}
start := 0
l := len(s)
last := l - 1
word := ""
str := ""
for i := 0; i < l; i++ {
if !m[string(s[i])] {
word = ""
for ; last >= 0; last-- {
if m[string(s[last])] && len(word) != i-start {
word += string(s[last])
} else if len(word) == i-start {
str += word
break
}
if last == 0 {
str += word
}
}
str += string(s[i])
start = i + 1
} else if i+1 == l {
word = ""
for ; last >= 0; last-- {
if m[string(s[last])] {
word += string(s[last])
}
if last == 0 {
str += word
}
}
}
}
return str
}
func main() {
v := reverseOnlyLetters("Test1ng-Leet=code-Q!")
fmt.Println(v)
//"j-Ih-gfE-dCba"
}