-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3823-ReverseLettersThenSpecialCharactersInAString.go
More file actions
124 lines (111 loc) · 3.92 KB
/
3823-ReverseLettersThenSpecialCharactersInAString.go
File metadata and controls
124 lines (111 loc) · 3.92 KB
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
119
120
121
122
123
124
package main
// 3823. Reverse Letters Then Special Characters in a String
// You are given a string s consisting of lowercase English letters and special characters.
// Your task is to perform these in order:
// 1. Reverse the lowercase letters and place them back into the positions originally occupied by letters.
// 2. Reverse the special characters and place them back into the positions originally occupied by special characters.
// Return the resulting string after performing the reversals.
// Example 1:
// Input: s = ")ebc#da@f("
// Output: "(fad@cb#e)"
// Explanation:
// The letters in the string are ['e', 'b', 'c', 'd', 'a', 'f']:
// Reversing them gives ['f', 'a', 'd', 'c', 'b', 'e']
// s becomes ")fad#cb@e("
// The special characters in the string are [')', '#', '@', '(']:
// Reversing them gives ['(', '@', '#', ')']
// s becomes "(fad@cb#e)"
// Example 2:
// Input: s = "z"
// Output: "z"
// Explanation:
// The string contains only one letter, and reversing it does not change the string. There are no special characters.
// Example 3:
// Input: s = "!@#$%^&*()"
// Output: ")(*&^%$#@!"
// Explanation:
// The string contains no letters. The string contains all special characters, so reversing the special characters reverses the whole string.
// Constraints:
// 1 <= s.length <= 100
// s consists only of lowercase English letters and the special characters in "!@#$%^&*()".
import "fmt"
// 双指针
func reverseByType(s string) string {
res := []byte(s)
reverse := func(t []byte, f func(byte) bool) {
i, j := 0, len(t)-1
for i < j {
for i < j && f(t[i]) {
i++
}
for i < j && f(t[j]) {
j--
}
t[i], t[j] = t[j], t[i]
i++
j--
}
}
reverse(res, func(b byte) bool { return 'a' <= b && b <= 'z' })
reverse(res, func(b byte) bool { return !('a' <= b && b <= 'z') })
return string(res)
}
func reverseByType1(s string) string {
n := len(s)
res := make([]byte, n)
ai, si := n-1, n-1
for ai >= 0 && s[ai] < 97 {
ai--
}
for si >= 0 && s[si] >= 97 {
si--
}
for i := range s {
if s[i] >= 97 {
res[ai] = s[i]
ai--
for ai >= 0 && s[ai] < 97 {
ai--
}
} else {
res[si] = s[i]
si--
for si >= 0 && s[si] >= 97 {
si--
}
}
}
return string(res)
}
func main() {
// Example 1:
// Input: s = ")ebc#da@f("
// Output: "(fad@cb#e)"
// Explanation:
// The letters in the string are ['e', 'b', 'c', 'd', 'a', 'f']:
// Reversing them gives ['f', 'a', 'd', 'c', 'b', 'e']
// s becomes ")fad#cb@e("
// The special characters in the string are [')', '#', '@', '(']:
// Reversing them gives ['(', '@', '#', ')']
// s becomes "(fad@cb#e)"
fmt.Println(reverseByType(")ebc#da@f(")) // "(fad@cb#e)"
// Example 2:
// Input: s = "z"
// Output: "z"
// Explanation:
// The string contains only one letter, and reversing it does not change the string. There are no special characters.
fmt.Println(reverseByType("z")) // "z"
// Example 3:
// Input: s = "!@#$%^&*()"
// Output: ")(*&^%$#@!"
// Explanation:
// The string contains no letters. The string contains all special characters, so reversing the special characters reverses the whole string.
fmt.Println(reverseByType("!@#$%^&*()")) // ")(*&^%$#@!"
fmt.Println(reverseByType("bluefrog")) // "gofreblu"
fmt.Println(reverseByType("leetcode")) // "edocteel"
fmt.Println(reverseByType1(")ebc#da@f(")) // "(fad@cb#e)"
fmt.Println(reverseByType1("z")) // "z"
fmt.Println(reverseByType1("!@#$%^&*()")) // ")(*&^%$#@!"
fmt.Println(reverseByType1("bluefrog")) // "gofreblu"
fmt.Println(reverseByType1("leetcode")) // "edocteel"
}