Skip to content

Commit a42e87b

Browse files
authored
Create minimum-number-of-frogs-croaking.cpp
1 parent fac3b74 commit a42e87b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int minNumberOfFrogs(string croakOfFrogs) {
7+
static const string S = "croak";
8+
vector<int> lookup(5);
9+
int result = 0;
10+
for (const auto& c : croakOfFrogs) {
11+
int i = S.find(c);
12+
++lookup[i];
13+
if (lookup[(i - 1 + lookup.size()) % lookup.size()]) {
14+
--lookup[(i - 1 + lookup.size()) % lookup.size()];
15+
} else if (i == 0) {
16+
++result;
17+
} else {
18+
return -1;
19+
}
20+
}
21+
return result == lookup.back() ? result : -1;
22+
}
23+
};

0 commit comments

Comments
 (0)