-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3775-ReverseWordsWithSameVowelCount.go
More file actions
144 lines (130 loc) · 4.46 KB
/
3775-ReverseWordsWithSameVowelCount.go
File metadata and controls
144 lines (130 loc) · 4.46 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
// 3775. Reverse Words With Same Vowel Count
// You are given a string s consisting of lowercase English words, each separated by a single space.
// Determine how many vowels appear in the first word.
// Then, reverse each following word that has the same vowel count.
// Leave all remaining words unchanged.
// Return the resulting string.
// Vowels are 'a', 'e', 'i', 'o', and 'u'.
// Example 1:
// Input: s = "cat and mice"
// Output: "cat dna mice"
// Explanation:
// The first word "cat" has 1 vowel.
// "and" has 1 vowel, so it is reversed to form "dna".
// "mice" has 2 vowels, so it remains unchanged.
// Thus, the resulting string is "cat dna mice".
// Example 2:
// Input: s = "book is nice"
// Output: "book is ecin"
// Explanation:
// The first word "book" has 2 vowels.
// "is" has 1 vowel, so it remains unchanged.
// "nice" has 2 vowels, so it is reversed to form "ecin".
// Thus, the resulting string is "book is ecin".
// Example 3:
// Input: s = "banana healthy"
// Output: "banana healthy"
// Explanation:
// The first word "banana" has 3 vowels.
// "healthy" has 2 vowels, so it remains unchanged.
// Thus, the resulting string is "banana healthy".
// Constraints:
// 1 <= s.length <= 10^5
// s consists of lowercase English letters and spaces.
// Words in s are separated by a single space.
// s does not contain leading or trailing spaces.
import "fmt"
import "strings"
import "slices"
func reverseWords(s string) string {
arr := strings.Split(s, " ")
countVowel := func(s string) int { // 统计元音个数
res := 0
for _, c := range s {
if strings.IndexRune("aeiou", c) >= 0 {
res++
}
}
return res
}
count := countVowel(arr[0]) // 第一个单词的元音数量
for i := 1; i < len(arr); i++ {
if countVowel(arr[i]) == count { // 如果它们的元音字母数与第一个单词相同,则将它们 反转
t := []byte(arr[i])
slices.Reverse(t)
arr[i] = string(t)
}
}
return strings.Join(arr, " ")
}
func reverseWords1(s string) string {
s += " "
count, index, n := 0, 0, len(s)
res := make([]byte, 0, n)
isVowel := func(c byte) bool { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' }
for ;; index++ {
ch := s[index]
if isVowel(ch) {
count++
}
if ch == ' ' {
break
}
res = append(res, ch)
}
count2, old := 0, index
for ; index < n; index++ {
ch := s[index]
if ch == ' ' {
if count2 == count {
for j := index-1; j >= old; j-- {
res = append(res, s[j])
}
} else {
res = append(res, []byte(s[old:index])...)
}
res = append(res, ' ')
count2 = 0
old = index+1
} else {
if isVowel(ch) {
count2++
}
}
}
return string(res[:n-1])
}
func main() {
// Example 1:
// Input: s = "cat and mice"
// Output: "cat dna mice"
// Explanation:
// The first word "cat" has 1 vowel.
// "and" has 1 vowel, so it is reversed to form "dna".
// "mice" has 2 vowels, so it remains unchanged.
// Thus, the resulting string is "cat dna mice".
fmt.Println(reverseWords("cat and mice")) // "cat dna mice"
// Example 2:
// Input: s = "book is nice"
// Output: "book is ecin"
// Explanation:
// The first word "book" has 2 vowels.
// "is" has 1 vowel, so it remains unchanged.
// "nice" has 2 vowels, so it is reversed to form "ecin".
// Thus, the resulting string is "book is ecin".
fmt.Println(reverseWords("book is nice")) // "book is ecin"
// Example 3:
// Input: s = "banana healthy"
// Output: "banana healthy"
// Explanation:
// The first word "banana" has 3 vowels.
// "healthy" has 2 vowels, so it remains unchanged.
// Thus, the resulting string is "banana healthy".
fmt.Println(reverseWords("banana healthy")) // "banana healthy"
fmt.Println(reverseWords("leetcode bluefrog")) // "leetcode bluefrog"
fmt.Println(reverseWords1("cat and mice")) // "cat dna mice"
fmt.Println(reverseWords1("book is nice")) // "book is ecin"
fmt.Println(reverseWords1("banana healthy")) // "banana healthy"
fmt.Println(reverseWords1("leetcode bluefrog")) // "leetcode bluefrog"
}