Skip to content

Commit d7692d7

Browse files
authored
Create 3016. Minimum Number of Pushes to Type Word II (#548)
2 parents 696ef67 + 4af0cc0 commit d7692d7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
// bool ccompare( pair<char, int>& a, pair<char, int>& b) {
4+
// return a.second > b.second;
5+
// }
6+
int minimumPushes(string word) {
7+
vector<pair<char, int>> freq(26);
8+
// initialising a vector of pairs which will store the alphabet and its
9+
// corresponding frequency
10+
for (int i = 0; i < 26; i++) {
11+
freq[i].first = 'a' + i;
12+
freq[i].second = 0;
13+
}
14+
// counting an aplhabets frequency
15+
for (int i = 0; i < word.length(); i++) {
16+
if (word[i] >= 'a' && word[i] <= 'z') {
17+
freq[word[i] - 'a'].second++;
18+
}
19+
}
20+
// sorting it in decreasing order (priorty to alphabet with high
21+
// frequency)
22+
sort(freq.begin(), freq.end(),
23+
[](pair<char, int>& a, pair<char, int>& b) {
24+
return a.second > b.second;
25+
});
26+
int push = 0;
27+
for (int i = 0; i < freq.size(); i++) {
28+
if (freq[i].second == 0)
29+
break;
30+
push += freq[i].second * ((i / 8) + 1);
31+
// we have 8 keys available (2 to 9), so every 8 characters, we
32+
// increase the number of pushes needed.
33+
}
34+
return push;
35+
}
36+
};

0 commit comments

Comments
 (0)