-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex.cpp
More file actions
74 lines (62 loc) · 1.72 KB
/
ex.cpp
File metadata and controls
74 lines (62 loc) · 1.72 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
#include<bits/stdc++.h>
using namespace std;
// vector<string> partitionString(string s) {
// vector<string> ans;
// unordered_set<string> seen;
// int i = 0;
// while(i < s.length()) {
// string current = "";
// // Build segment character by character until it's unique
// do {
// current += s[i];
// i++;
// } while(i < s.length() && seen.count(current) > 0);
// // Only add if we found a unique segment OR if we've consumed all characters
// // but the segment is still unique
// if(seen.count(current) == 0) {
// ans.push_back(current);
// seen.insert(current);
// }
// }
// return ans;
// }
vector<string> partitionString(string s) {
unordered_set<string> seen;
vector<string> ans;
string t;
for (char c : s) {
t.push_back(c);
if (!seen.count(t)) {
ans.push_back(t);
seen.insert(t);
t.clear();
}
}
return ans;
}
vector<string> partition(string s){
int n = s.length();
unordered_set<string> seen;
vector<string> ans;
string curr = "";
for(char c:s){
curr.push_back(c);
if(!seen.count(curr)){
ans.push_back(curr);
seen.insert(curr);
curr.clear();
}
}
return ans;
}
int main(){
vector<string> ans = partition("aaabbbccccdd");
// Print the result
cout << "Result: [";
for(int i = 0; i < ans.size(); i++) {
cout << "\"" << ans[i] << "\"";
if(i < ans.size() - 1) cout << ", ";
}
cout << "]" << endl;
return 0;
}