-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_46.java
More file actions
52 lines (49 loc) · 1.41 KB
/
Copy pathDay_46.java
File metadata and controls
52 lines (49 loc) · 1.41 KB
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
52
// Shortest Palindrome
class Solution {
public String shortestPalindrome(String s) {
int n = s.length();
StringBuilder sb = new StringBuilder(s).reverse();
String rev = sb.toString();
String str = s + "$" + rev;
int size = str.length(), len = 0, i = 1;
int[] lps = new int[size];
while(i<size){
if(str.charAt(len) == str.charAt(i)){
len++;
lps[i] = len;
i++;
}
else{
if(len != 0){
len = lps[len-1];
}
else{
lps[i] = 0;
i++;
}
}
}
return rev.substring(0,rev.length()-lps[size-1]) + s;
}
}
// Fruit Into Baskets
class Solution {
public int totalFruit(int[] fruits) {
int n = fruits.length;
if(n == 1) return 1;
Map<Integer,Integer> mpp = new HashMap<>();
int l = 0,maxcnt = 0;
for(int r=0;r<n;r++){
mpp.put(fruits[r],mpp.getOrDefault(fruits[r],0)+1);
if(mpp.size()>2){
int val = fruits[l];
int freq = mpp.get(val);
if(freq - 1 == 0) mpp.remove(val);
else mpp.put(val,freq-1);
l++;
}
if(mpp.size()<=2) maxcnt = Math.max(maxcnt,r-l+1);
}
return maxcnt;
}
}