forked from emotionless/Google_interview_question_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathways_to_splilt.cpp
46 lines (40 loc) · 941 Bytes
/
ways_to_splilt.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <map>
#include <unordered_map>
using namespace std;
int numOfUniqueChar(string str) {
unordered_map<char, bool> vis;
for(auto c : str) {
vis[c] = true;
}
return vis.size();
}
int waysToSplit(string str) {
unordered_map<char, int> counter1, counter2;
int n = numOfUniqueChar(str);
for(int i = 0; i < str.length(); i++) {
counter2[str[i]]++;
}
int ans = 0;
for(auto c : str) {
counter2[c]--;
if (counter2[c] == 0) {
counter2.erase(c);
}
counter1[c]++;
if (counter1.size() == counter2.size()) {
ans++;
}
// cout << c << " " << ans << endl;
}
return ans;
}
int main() {
cout << waysToSplit("ababa") << endl;
cout << waysToSplit("aba") << endl;
cout << waysToSplit("aaaa") << endl;
return 0;
}