53
53
54
54
<!-- 这里可写通用的实现逻辑 -->
55
55
56
- 维护 croak 的个数,如果遇到当前字母,则肯定是由前面字母过来,前面字母数减 1。如遇到 r,则必是 ` c->r ` ,所以 c 减 1。
56
+ ** 方法一:计数 **
57
57
58
- k 代表结尾,表示一次喊叫结束,所以遇到 c 的时候,先去消耗 k,没有 k 了,需要新青蛙,ans 加 1。
58
+ 我们注意到,如果字符串 ` croakOfFrogs ` 是由若干有效的 ` "croak" ` 字符混合而成,那么它的长度一定是 $5$ 的倍数。因此,如果字符串的长度不是 $5$ 的倍数,可以直接返回 $-1$。
59
+
60
+ 接下来,我们将 ` 'c' ` , ` 'r' ` , ` 'o' ` , ` 'a' ` , ` 'k' ` 这 $5$ 个字母分别对应下标 $0$ 到 $4$,用一个长度为 $5$ 的数组 $cnt$ 记录字符串 ` croakOfFrogs ` 中每个字母出现的次数,其中 $cnt[ i] $ 表示当前下标为 $i$ 的字母出现的次数。另外,我们定义一个整数变量 $x$ 表示当前未完成蛙鸣的青蛙的数目,需要的青蛙的最少数目 $ans$ 即为 $x$ 的最大值。
61
+
62
+ 我们遍历字符串 ` croakOfFrogs ` 中的每个字母 $c$,找到 $c$ 对应的下标 $i$,然后将 $cnt[ i] $ 加 $1$。接下来,根据 $i$ 值的不同,我们分别进行如下操作:
63
+
64
+ - 如果 $i=0$,那么当前有一个新的青蛙开始蛙鸣,因此令 $x$ 的值加 $1$,然后我们更新 $ans = \max(ans, x)$;
65
+ - 否则,如果 $cnt[ i-1] =0$,那么表示当前没有青蛙可以发出字母 $c$,无法完成蛙鸣,返回 $-1$,否则我们令 $cnt[ i-1] $ 减 $1$。如果 $i=4$,那么表示青蛙已经完成了一个蛙鸣,因此令 $x$ 的值减 $1$。
66
+
67
+ 遍历结束后,如果 $x=0$,那么说明青蛙已经完成了所有的蛙鸣,返回 $ans$,否则返回 $-1$。
68
+
69
+ 时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 ` croakOfFrogs ` 的长度;而 $C$ 是字符集的大小,本题中 $C=26$。
59
70
60
71
<!-- tabs:start -->
61
72
@@ -66,29 +77,23 @@ k 代表结尾,表示一次喊叫结束,所以遇到 c 的时候,先去消
66
77
``` python
67
78
class Solution :
68
79
def minNumberOfFrogs (self , croakOfFrogs : str ) -> int :
69
- c = r = o = a = k = ans = 0
70
- for ch in croakOfFrogs:
71
- if ch == ' c' :
72
- c += 1
73
- if k > 0 :
74
- k -= 1
75
- else :
76
- ans += 1
77
- elif ch == ' r' :
78
- r += 1
79
- c -= 1
80
- elif ch == ' o' :
81
- o += 1
82
- r -= 1
83
- elif ch == ' a' :
84
- a += 1
85
- o -= 1
80
+ if len (croakOfFrogs) % 5 != 0 :
81
+ return - 1
82
+ idx = {c: i for i, c in enumerate (' croak' )}
83
+ cnt = [0 ] * 5
84
+ ans = x = 0
85
+ for i in map (idx.get, croakOfFrogs):
86
+ cnt[i] += 1
87
+ if i == 0 :
88
+ x += 1
89
+ ans = max (ans, x)
86
90
else :
87
- k += 1
88
- a -= 1
89
- if c < 0 or r < 0 or o < 0 or a < 0 :
90
- return - 1
91
- return - 1 if c != 0 or r != 0 or o != 0 or a != 0 else ans
91
+ if cnt[i - 1 ] == 0 :
92
+ return - 1
93
+ cnt[i - 1 ] -= 1
94
+ if i == 4 :
95
+ x -= 1
96
+ return - 1 if x else ans
92
97
```
93
98
94
99
### ** Java**
@@ -98,34 +103,32 @@ class Solution:
98
103
``` java
99
104
class Solution {
100
105
public int minNumberOfFrogs (String croakOfFrogs ) {
101
- int c = 0 , r = 0 , o = 0 , a = 0 , k = 0 ;
102
- int ans = 0 ;
103
- for (char ch : croakOfFrogs. toCharArray()) {
104
- if (ch == ' c' ) {
105
- ++ c;
106
- if (k > 0 ) {
107
- -- k;
108
- } else {
109
- ++ ans;
110
- }
111
- } else if (ch == ' r' ) {
112
- ++ r;
113
- -- c;
114
- } else if (ch == ' o' ) {
115
- ++ o;
116
- -- r;
117
- } else if (ch == ' a' ) {
118
- ++ a;
119
- -- o;
106
+ int n = croakOfFrogs. length();
107
+ if (n % 5 != 0 ) {
108
+ return - 1 ;
109
+ }
110
+ int [] idx = new int [26 ];
111
+ String s = " croak" ;
112
+ for (int i = 0 ; i < 5 ; ++ i) {
113
+ idx[s. charAt(i) - ' a' ] = i;
114
+ }
115
+ int [] cnt = new int [5 ];
116
+ int ans = 0 , x = 0 ;
117
+ for (int k = 0 ; k < n; ++ k) {
118
+ int i = idx[croakOfFrogs. charAt(k) - ' a' ];
119
+ ++ cnt[i];
120
+ if (i == 0 ) {
121
+ ans = Math . max(ans, ++ x);
120
122
} else {
121
- ++ k;
122
- -- a;
123
- }
124
- if (c < 0 || r < 0 || o < 0 || a < 0 ) {
125
- return - 1 ;
123
+ if (-- cnt[i - 1 ] < 0 ) {
124
+ return - 1 ;
125
+ }
126
+ if (i == 4 ) {
127
+ -- x;
128
+ }
126
129
}
127
130
}
128
- return c == 0 && r == 0 && o == 0 && a == 0 ? ans : - 1 ;
131
+ return x > 0 ? - 1 : ans ;
129
132
}
130
133
}
131
134
```
@@ -136,30 +139,32 @@ class Solution {
136
139
class Solution {
137
140
public:
138
141
int minNumberOfFrogs(string croakOfFrogs) {
139
- int c = 0, r = 0, o = 0, a = 0, k = 0, ans = 0;
140
- for (char ch : croakOfFrogs) {
141
- if (ch == 'c') {
142
- ++c;
143
- if (k > 0)
144
- --k;
145
- else
146
- ++ans;
147
- } else if (ch == 'r') {
148
- ++r;
149
- --c;
150
- } else if (ch == 'o') {
151
- ++o;
152
- --r;
153
- } else if (ch == 'a') {
154
- ++a;
155
- --o;
142
+ int n = croakOfFrogs.size();
143
+ if (n % 5 != 0) {
144
+ return -1;
145
+ }
146
+ int idx[ 26] {};
147
+ string s = "croak";
148
+ for (int i = 0; i < 5; ++i) {
149
+ idx[ s[ i] - 'a'] = i;
150
+ }
151
+ int cnt[ 5] {};
152
+ int ans = 0, x = 0;
153
+ for (char& c : croakOfFrogs) {
154
+ int i = idx[ c - 'a'] ;
155
+ ++cnt[ i] ;
156
+ if (i == 0) {
157
+ ans = max(ans, ++x);
156
158
} else {
157
- ++k;
158
- --a;
159
+ if (--cnt[ i - 1] < 0) {
160
+ return -1;
161
+ }
162
+ if (i == 4) {
163
+ --x;
164
+ }
159
165
}
160
- if (c < 0 || r < 0 || o < 0 || a < 0) return -1;
161
166
}
162
- return c == 0 && r == 0 && o == 0 && a == 0 ? ans : -1 ;
167
+ return x > 0 ? -1 : ans ;
163
168
}
164
169
};
165
170
```
@@ -168,37 +173,73 @@ public:
168
173
169
174
```go
170
175
func minNumberOfFrogs(croakOfFrogs string) int {
171
- c, r, o, a, k, ans := 0, 0, 0, 0, 0, 0
172
- for i := range croakOfFrogs {
173
- ch := croakOfFrogs[i]
174
- if ch == 'c' {
175
- c++
176
- if k > 0 {
177
- k--
178
- } else {
179
- ans++
180
- }
181
- } else if ch == 'r' {
182
- r++
183
- c--
184
- } else if ch == 'o' {
185
- o++
186
- r--
187
- } else if ch == 'a' {
188
- a++
189
- o--
176
+ n := len(croakOfFrogs)
177
+ if n%5 != 0 {
178
+ return -1
179
+ }
180
+ idx := [26]int{}
181
+ for i, c := range "croak" {
182
+ idx[c-'a'] = i
183
+ }
184
+ cnt := [5]int{}
185
+ ans, x := 0, 0
186
+ for _, c := range croakOfFrogs {
187
+ i := idx[c-'a']
188
+ cnt[i]++
189
+ if i == 0 {
190
+ x++
191
+ ans = max(ans, x)
190
192
} else {
191
- k++
192
- a--
193
- }
194
- if c < 0 || r < 0 || o < 0 || a < 0 {
195
- return -1
193
+ cnt[i-1]--
194
+ if cnt[i-1] < 0 {
195
+ return -1
196
+ }
197
+ if i == 4 {
198
+ x--
199
+ }
196
200
}
197
201
}
198
- if c == 0 && r == 0 && o == 0 && a == 0 {
199
- return ans
202
+ if x > 0 {
203
+ return -1
200
204
}
201
- return -1
205
+ return ans
206
+ }
207
+
208
+ func max(a, b int) int {
209
+ if a > b {
210
+ return a
211
+ }
212
+ return b
213
+ }
214
+ ```
215
+
216
+ ### ** TypeScript**
217
+
218
+ ``` ts
219
+ function minNumberOfFrogs(croakOfFrogs : string ): number {
220
+ const n = croakOfFrogs .length ;
221
+ if (n % 5 !== 0 ) {
222
+ return - 1 ;
223
+ }
224
+ const idx = (c : string ): number => ' croak' .indexOf (c );
225
+ const cnt: number [] = [0 , 0 , 0 , 0 , 0 ];
226
+ let ans = 0 ;
227
+ let x = 0 ;
228
+ for (const c of croakOfFrogs ) {
229
+ const i = idx (c );
230
+ ++ cnt [i ];
231
+ if (i === 0 ) {
232
+ ans = Math .max (ans , ++ x );
233
+ } else {
234
+ if (-- cnt [i - 1 ] < 0 ) {
235
+ return - 1 ;
236
+ }
237
+ if (i === 4 ) {
238
+ -- x ;
239
+ }
240
+ }
241
+ }
242
+ return x > 0 ? - 1 : ans ;
202
243
}
203
244
```
204
245
0 commit comments