Day 41 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: Permutations
Link to the problem: https://leetcode.com/problems/permutations/

class Solution {
public List<List<Integer>> permute(int[] nums){
List<List<Integer>> res = new ArrayList<>();
backtrack(nums, 0, res);
return res;
}
private void backtrack(int[] nums, int start, List<List<Integer>> res){
if(start == nums.length){
res.add(arrayToList(nums));
return;
}
for(int i = start; i < nums.length; i++){
swap(nums, start, i);
backtrack(nums, start + 1, res);
swap(nums, start, i);
}
}
private List<Integer> arrayToList(int[] arr){
List<Integer> list = new ArrayList<>();
for(int num : arr) list.add(num);
return list;
}
private void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
Problem 2: Letter Combination of a Phone
Link to the problem: https://leetcode.com/problems/letter-combinations-of-a-phone-number/

class Solution {
static HashMap<Character, char[]> map = new HashMap<>();
static List<String> res = new ArrayList<>();
public List<String> letterCombinations(String digits) {
res.clear();
if(digits == null || digits.length() == 0) return res;
map.put('2', new char[]{'a', 'b', 'c'});
map.put('3', new char[]{'d', 'e', 'f'});
map.put('4', new char[]{'g', 'h', 'i'});
map.put('5', new char[]{'j', 'k', 'l'});
map.put('6', new char[]{'m', 'n', 'o'});
map.put('7', new char[]{'p', 'q', 'r', 's'});
map.put('8', new char[]{'t', 'u', 'v'});
map.put('9', new char[]{'w', 'x', 'y', 'z'});
helper("", digits, 0);
return res;
}
private void helper(String str, String digits, int i){
if(digits.length()==0){
res.add(str);
return;
}
char[] charray = map.get(digits.charAt(0));
if(i>=charray.length) return;
helper(str.concat(Character.toString(charray[i])), digits.substring(1), 0);
helper(str, digits, i+1);
}
}
Problem 3: Generate Parentheses
Link to the problem: https://leetcode.com/problems/generate-parentheses/

class Solution {
public List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<>();
helper(res, "", n, 0);
return res;
}
private void helper(List<String> res, String str, int n, int m){
if(n==0 && m==0){
res.add(str);
return;
}
if(m==0){
helper(res, str.concat("("), n-1, 1);
return;
}
if(n==0){
while(m--!=0)
str = str.concat(")");
res.add(str);
return;
}
helper(res, str.concat("("), n-1, m+1);
helper(res, str.concat(")"), n, m-1);
return;
}
}
Problem 4: Delete Nodes From Linked List Present in Array
Link to the problem: https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode modifiedList(int[] nums, ListNode head) {
if(head==null) return null;
HashMap<Integer, Integer> map = new HashMap<>();
for(int i:nums) map.put(i, 0);
while(head!=null && map.containsKey(head.val)) head = head.next;
ListNode ptr = head;
while(ptr!=null && ptr.next!=null){
if(map.containsKey(ptr.next.val)) ptr.next = ptr.next.next;
else ptr = ptr.next;
}
return head;
}
}
Problem 5: Next Permutation
Link to the problem: https://leetcode.com/problems/next-permutation/

class Solution {
public void nextPermutation(int[] nums){
int i = nums.length - 1;
while(i > 0 && nums[i - 1] >= nums[i]) i--;
if(i == 0){
reverse(nums, 0, nums.length - 1);
return;
}
int j = nums.length - 1;
while(j >= i && nums[j] <= nums[i - 1]) j--;
swap(nums, i - 1, j);
reverse(nums, i, nums.length - 1);
}
private void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
private void reverse(int[] nums, int start, int end){
while (start < end){
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}



