21BCS7264 Prateek Day19 Java
21BCS7264 Prateek Day19 Java
21BCS7264 Prateek Day19 Java
Code:
class Solution {
public boolean rotateString(String s, String goal) {
return s.length() == goal.length() && (s+s).contains(goal);
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
class Solution {
public int mostWordsFound(String[] arr) {
int max = -1;
int n = arr.length;
for(int i=0; i<n; i++){
max = Math.max(max,arr[i].split(" ").length);
}
return max;
}
}
Output:
Code:
class FreqStack {
Map<Integer, Integer> map;
int idx;
PriorityQueue<int[]> pq;
public FreqStack() {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
map = new HashMap<>();
pq = new PriorityQueue<>(
(a, b) -> {
int t = b[2] - a[2];
if (t != 0) {
return t;
}
return b[1] - a[1];
}
);
}
public void push(int val) {
map.put(val, map.getOrDefault(val, 0) + 1);
idx++;
pq.add(new int[]{val, idx, map.get(val)});
}
public int pop() {
int val = pq.poll()[0];
if (map.get(val) == 1) {
map.remove(val);
} else {
map.put(val, map.get(val) - 1);
}
return val;
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> ds = new ArrayList<>();
Arrays.sort(candidates);
combination(candidates, target, ans, ds, 0);
return ans;
}
public void combination(int[] candidates, int target, List<List<Integer>> ans, List<Integer> ds, int
index) {
if(target == 0) {
ans.add(new ArrayList<>(ds));
return ;
}
for(int i = index ; i < candidates.length; i++) {
if(i > index && candidates[i] == candidates[i-1]) continue;
if(candidates[i] > target) break;
ds.add(candidates[i]);
combination(candidates, target - candidates[i], ans, ds, i + 1);
ds.remove(ds.size() - 1);
}
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
class Solution {
public String minWindow(String s, String t) {
if (s == null || t == null || s.length() ==0 || t.length() == 0 ||
s.length() < t.length()) {
return new String();
}
int[] map = new int[128];
int count = t.length();
int start = 0, end = 0, minLen = Integer.MAX_VALUE,startIndex =0;
for (char c :t.toCharArray()) {
map[c]++;
}
char[] chS = s.toCharArray();
while (end < chS.length) {
if (map[chS[end++]]-- >0) {
count--;
}
while (count == 0) {
if (end - start < minLen) {
startIndex = start;
minLen = end - start;
}
if (map[chS[start++]]++ == 0) {
count++;
}
}
}
return minLen == Integer.MAX_VALUE? new String():
new String(chS,startIndex,minLen);
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
class Solution {
public int getSum(int a, int b) {
while ((a&b) != 0) {
int olda = a;
a = olda^b;
b = (olda&b) << 1;
}
return a|b;
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING