forked from Nanduag0/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCanMakePalindromeFromSubstring.txt
51 lines (46 loc) · 1.09 KB
/
CanMakePalindromeFromSubstring.txt
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
class Solution {
public:
vector<bool> canMakePaliQueries(string s, vector<vector<int>>& queries)
{
//have to use DP ! ! ! ! !
vector<int> vec(26,0);
vector<vector<int>> prefix;
for(char c : s)
{
vec[c-'a']++;
prefix.push_back(vec);
}
vector<bool> ans;
for(int p=0;p<queries.size();p++)
{
int l=queries[p][0];
int h=queries[p][1];
int changes=queries[p][2];
vector<int> curr(26,0);
if(l==0)
{
curr=prefix[h];
}
else
{
for(int k=0;k<26;k++)
{
curr[k]=prefix[h][k]-prefix[l-1][k];
}
}
int val =cnt(curr);
ans.push_back(val<=changes);
}
return ans;
}
int cnt(vector<int> v)
{
int count=0;
for(int i=0;i<v.size();i++)
{
if(v[i]&1)
count++;
}
return count/2;
}
};