-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1957-DeleteCharactersToMakeFancyString.go
More file actions
100 lines (87 loc) · 3.11 KB
/
1957-DeleteCharactersToMakeFancyString.go
File metadata and controls
100 lines (87 loc) · 3.11 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
package main
// 1957. Delete Characters to Make Fancy String
// A fancy string is a string where no three consecutive characters are equal.
// Given a string s, delete the minimum possible number of characters from s to make it fancy.
// Return the final string after the deletion. It can be shown that the answer will always be unique.
// Example 1:
// Input: s = "leeetcode"
// Output: "leetcode"
// Explanation:
// Remove an 'e' from the first group of 'e's to create "leetcode".
// No three consecutive characters are equal, so return "leetcode".
// Example 2:
// Input: s = "aaabaaaa"
// Output: "aabaa"
// Explanation:
// Remove an 'a' from the first group of 'a's to create "aabaaaa".
// Remove two 'a's from the second group of 'a's to create "aabaa".
// No three consecutive characters are equal, so return "aabaa".
// Example 3:
// Input: s = "aab"
// Output: "aab"
// Explanation: No three consecutive characters are equal, so return "aab".
// Constraints:
// 1 <= s.length <= 10^5
// s consists only of lowercase English letters.
import "fmt"
func makeFancyString(s string) string {
res, n := []byte{}, len(s)
if n <= 2 { return s }
for i := 0; i < n - 2; i++ {
if s[i] == s[i + 1] && s[i] == s[i + 2] { // 不能 3 个相同字符
continue
}
res = append(res, s[i])
}
// 追加后面两个字符
res = append(res, s[n - 2])
res = append(res, s[n - 1])
return string(res)
}
func makeFancyString1(s string) string {
arr := []byte(s)
candidate := arr[0]
count, i, j, n := 0, 0, 0, len(arr)
for ; j < n; j++ {
if candidate != arr[j] { // 不是连续的
count = 0
candidate = arr[j]
}
count++
if count > 2 { continue }
arr[i] = arr[j]
i++
}
return string(arr[:i])
}
func main() {
// Example 1:
// Input: s = "leeetcode"
// Output: "leetcode"
// Explanation:
// Remove an 'e' from the first group of 'e's to create "leetcode".
// No three consecutive characters are equal, so return "leetcode".
fmt.Println(makeFancyString("leeetcode")) // leetcode
// Example 2:
// Input: s = "aaabaaaa"
// Output: "aabaa"
// Explanation:
// Remove an 'a' from the first group of 'a's to create "aabaaaa".
// Remove two 'a's from the second group of 'a's to create "aabaa".
// No three consecutive characters are equal, so return "aabaa".
fmt.Println(makeFancyString("aaabaaaa")) // aabaa
// Example 3:
// Input: s = "aab"
// Output: "aab"
// Explanation: No three consecutive characters are equal, so return "aab".
fmt.Println(makeFancyString("aab")) // aab
fmt.Println(makeFancyString("a")) // a
fmt.Println(makeFancyString("bluefrog")) // bluefrog
fmt.Println(makeFancyString("freewu")) // a
fmt.Println(makeFancyString1("leeetcode")) // leetcode
fmt.Println(makeFancyString1("aaabaaaa")) // aabaa
fmt.Println(makeFancyString1("aab")) // aab
fmt.Println(makeFancyString1("a")) // a
fmt.Println(makeFancyString1("bluefrog")) // bluefrog
fmt.Println(makeFancyString1("freewu")) // freewu
}