-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-1177.cpp
More file actions
29 lines (26 loc) · 923 Bytes
/
Problem-1177.cpp
File metadata and controls
29 lines (26 loc) · 923 Bytes
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
//Problem - 1177
// https://leetcode.com/problems/can-make-palindrome-from-substring/
// O(s.length() + queries.length()) comlexity using presum computation
// O(s.lenght()) space complexity
class Solution {
public:
vector<bool> canMakePaliQueries(string s, vector<vector<int>>& queries) {
vector <vector <int>> pre_sum(26, vector <int> (s.size(), 0));
vector <bool> ans;
for(int i = 0; i < s.length(); i++) {
for(int j = 0; j < 26; j++)
pre_sum[j][i] += i > 0 ? pre_sum[j][i-1] : 0;
pre_sum[s[i] - 'a'][i]++;
}
for(auto q : queries) {
int sum = 0;
for(int j = 0; j < 26; j++) {
int t = pre_sum[j][q[1]] - (q[0] == 0 ? 0 : pre_sum[j][q[0]-1]);
if(t & 1)
sum ++;
}
ans.push_back(sum/2 <= q[2]);
}
return ans;
}
};