-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLongEncodedString.cpp
More file actions
48 lines (41 loc) · 1.07 KB
/
Copy pathLongEncodedString.cpp
File metadata and controls
48 lines (41 loc) · 1.07 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
// All test cases passed.
int getCount(string s, int& i){
string ans = "";
while(i < s.size() && s[i] != ')'){
ans = ans + s[i];
i++;
}
return stoi(ans);
}
vector < int > frequency(string s) {
vector<int> ans(26, 0);
int i = 0, n = s.size();
while(i < n){
if(i+2 < n && s[i+2] == '#'){
int num = stoi(to_string(s[i] - '0') + to_string(s[i+1] - '0')), count = 0;
if(i+3 < n && s[i+3] == '('){
i = i + 4;
count = getCount(s, i);
i++;
}
else{
count = 1;
i = i + 3;
}
ans[num-1] = ans[num-1] + count;
}
else if(i+1 < n && s[i+1] == '('){
int num = s[i] - '0', count = 0;
i = i + 2;
count = getCount(s, i);
ans[num-1] = ans[num-1] + count;
i++;
}
else{
int num = s[i] - '0';
ans[num-1] = ans[num-1] + 1;
i++;
}
}
return ans;
}