Day 37 of LeetCode Challenge

Cloud and DevOps Engineer with hands-on expertise in AWS, CI/CD pipelines, Docker, Kubernetes, and Monitoring tools. Adept at building and automating scalable, fault-tolerant cloud infrastructures, and consistently improving system performance, security, and reliability in dynamic environments.
Problem 1: Minimum Window Substring
Link to the problem: https://leetcode.com/problems/minimum-window-substring/

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);
}
}
Problem 2: Palindrome Pairs
Link to the problem: https://leetcode.com/problems/palindrome-pairs/

public class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (words == null || words.length == 0)
return res;
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < words.length; i++)
map.put(words[i], i);
if (map.containsKey("")) {
int blankIdx = map.get("");
for (int i = 0; i < words.length; i++) {
if (isPalindrome(words[i])) {
if (i == blankIdx)
continue;
res.add(Arrays.asList(blankIdx, i));
res.add(Arrays.asList(i, blankIdx));
}
}
}
for (int i = 0; i < words.length; i++) {
String cur_r = reverseStr(words[i]);
if (map.containsKey(cur_r)) {
int found = map.get(cur_r);
if (found == i)
continue;
res.add(Arrays.asList(i, found));
}
}
for (int i = 0; i < words.length; i++) {
String cur = words[i];
for (int cut = 1; cut < cur.length(); cut++) {
if (isPalindrome(cur.substring(0, cut))) {
String cut_r = reverseStr(cur.substring(cut));
if (map.containsKey(cut_r)) {
int found = map.get(cut_r);
if (found == i)
continue;
res.add(Arrays.asList(found, i));
}
}
if (isPalindrome(cur.substring(cut))) {
String cut_r = reverseStr(cur.substring(0, cut));
if (map.containsKey(cut_r)) {
int found = map.get(cut_r);
if (found == i)
continue;
res.add(Arrays.asList(i, found));
}
}
}
}
return res;
}
public String reverseStr(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
public boolean isPalindrome(String s){
int i = 0;
int j = s.length() - 1;
while(i <= j){
if(s.charAt(i) != s.charAt(j)) return false;
i++;
j--;
}
return true;
}
}
Problem 3: Binary Tree Level Order Traversal
Link to the problem: https://leetcode.com/problems/binary-tree-level-order-traversal/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> al = new ArrayList<>();
pre(root, 0, al);
return al;
}
public static void pre(TreeNode root, int l, List<List<Integer>> al) {
if(root == null)
return;
if(al.size() == l) {
List<Integer> li = new ArrayList<>();
li.add(root.val);
al.add(li);
}else
al.get(l).add(root.val);
pre(root.left, l + 1, al);
pre(root.right, l + 1, al);
}
}
Problem 4: Valid Word
Link to the problem: https://leetcode.com/problems/valid-word/

class Solution {
public boolean isValid(String word) {
return word.matches("(?i)(?=^.*[b-df-hj-np-tv-z])(?=.*[aieou])^[a-z0-9]{3,}$");
}
}
Problem 5: Search in Rotated Sorted Array
Link to the problem: https://leetcode.com/problems/search-in-rotated-sorted-array/

class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while(left <= right) {
int mid = (left + right) / 2;
if(nums[mid] == target) return mid;
else if(nums[mid] >= nums[left]) {
if(nums[left] <= target && target <= nums[mid]) right = mid - 1;
else left = mid + 1;
}else{
if(nums[mid] <= target && target <= nums[right]) left = mid + 1;
else right = mid - 1;
}
}
return -1;
}
}




