Skip to content

Commit c36b29c

Browse files
committed
Runtime: 29 ms (Top 84.38%) | Memory: 106.10 MB (Top 54.69%)
1 parent b18a807 commit c36b29c

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

scripts/algorithms/C/Can Make Palindrome from Substring/Can Make Palindrome from Substring.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
// Runtime: 29 ms (Top 84.38%) | Memory: 106.10 MB (Top 54.69%)
2+
13
class Solution
2-
{
3-
public List<Boolean> canMakePaliQueries(String s, int[][] queries)
4+
{ public List<Boolean> canMakePaliQueries(String s, int[][] queries)
45
{
56
List<Boolean> list = new ArrayList<>();
67

78
int n = s.length();
9+
// prefix map to count number of time each letters have occured to access in queries in O(1)
10+
//s= a b c d a
11+
// a 1 1 1 1 2
12+
// b 0 1 1 1 1
13+
// c 0 0 1 1 1
14+
// d 0 0 0 1 1
15+
// e
16+
// f
17+
// .
18+
// .
19+
// .
820
int[][] map = new int[n+1][26];
9-
1021
for(int i=0;i<s.length();i++)
1122
{
1223
for(int j=0;j<26;j++)
@@ -23,9 +34,9 @@ public List<Boolean> canMakePaliQueries(String s, int[][] queries)
2334
int count = 0;
2435

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

28-
list.add(count/2 <= k);
39+
list.add(count/2 <= k);// if we can replace half of them with their pair
2940
}
3041

3142
return list;

0 commit comments

Comments
 (0)