Can Make Palindrome from Substring LeetCode solution in Java

1
class Solution {
2
public List<Boolean> canMakePaliQueries(String s, int[][] queries) {
3
List<Boolean> list = new ArrayList<>();
4

5
int n = s.length();
6
// prefix map to count number of time each letters have occured to access in queries in O(1)
7
// s= a b c d a
8
// a 1 1 1 1 2
9
// b 0 1 1 1 1
10
// c 0 0 1 1 1
11
// d 0 0 0 1 1
12
// e
13
// f
14
// .
15
// .
16
// .
17
int[][] map = new int[n + 1][26];
18
for (int i = 0; i < s.length(); i++) {
19
for (int j = 0; j < 26; j++) map[i + 1][j] = map[i][j];
20

21
map[i + 1][s.charAt(i) - 'a']++;
22
}
23

24
for (int[] q : queries) {
25
int l = q[0];
26
int r = q[1];
27
int k = q[2];
28
int count = 0;
29

30
for (int i = 0; i < 26; i++)
31
count +=
32
(map[r + 1][i] - map[l][i])
33
% 2; // count total characters that have odd count, so that we can replace the half
34
// of them with their pair.
35

36
list.add(count / 2 <= k); // if we can replace half of them with their pair
37
}
38

39
return list;
40
}
41
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0