├── String ├── valid Anagram.java ├── Rotate String.java ├── Reverse String .java ├── Is Subsequence.java ├── Find Words Containing Character.java ├── Find the Difference.java ├── To Lower Case.java ├── Check If Two String Arrays are Equivalent.java ├── Longest Common Prefix.java ├── Find the Index of the First Occurrence.java ├── Reverse String II.java ├── Longest Substring Without Repeating Characters.java ├── Group Anagrams.java ├── FizzBuzz.java ├── First Unique Character in a String.java ├── Reverse a letter in string.java ├── valid palindrome.java ├── valid word.java ├── Reverse vowel of a string.java ├── Valid Paranthesis.java ├── Roman to Integer.java └── Multiply Strings.java ├── Linked List ├── Singly Linked List │ ├── Delete a node from a LL.java │ ├── Reverse a Linkdlist Recursive.java │ ├── middle of the linkedlist.java │ ├── Linked List Cycle.java │ ├── Reverse a Linked list Iterative.java │ ├── Remove Duplicates from sorted list II.java │ ├── Delete middle node of a linkedlist.java │ ├── merge two sorted list recursive.java │ ├── intersection of two linkedlist.java │ ├── merge two sorted list iterative.java │ ├── Remove duplicate from sorted list.java │ ├── Add two numbers.java │ ├── Remove nth node from the end of the list.java │ └── Palindromic Linked List.java └── Doubly Linked List │ └── Flatten a Multilevel Doubly Linked List.java ├── Array ├── singlenumbe.java ├── Shuffle the Array.java ├── Array Partition.java ├── Runningsumof1Darray.java ├── Concatenation of Array.java ├── find duplicate.java ├── PalindromeNumber.java ├── find the highest altitude.java ├── maximumsubarray.java ├── besttimetobuyandsellstock.java ├── Maximum Product of Three Numbers.java ├── RemoveDuplicateI.java ├── movezeros.java ├── remove element.java ├── max69num.java ├── Contains Duplicate.java ├── besttimetobuyandsellstock2.java ├── Subarray Sum Equals K.java ├── checkifarrayissortedandrotated.java ├── Majority Element.java ├── find peak element .java ├── Unique number of occurances.java ├── findpivotindex.java ├── containerwithmostwater.java ├── findthemiddleindexinarray.java ├── findduplicatenumber.java ├── Two Sum.java ├── binarysearch.java ├── maximumproductsubarray.java ├── Range sum query- immutable.java ├── twosumII.java ├── sqauresofsortedaraay.java ├── minimum size subarray sum .java ├── searchinsertposition.java ├── validpalindrome.java ├── productofarrayexceptitself.java ├── Missingnumber.java ├── intersection of two arrays.java ├── Find duplicates number in an array.java ├── Searchinrotatedsortedarray.java ├── Intersection of two Array II .java ├── Trapping Rain Water.java ├── Kthlargestelementinarray.java ├── maximum average subarray I .java ├── firstandlastpositionofelementinsortedarray.java ├── Rotate Array.java └── longest consecutive sequence.java ├── Bit Manipulation └── singlenumbe.java ├── Prefix Sum ├── Runningsumof1Darray.java ├── find the highest altitude.java ├── findpivotindex.java ├── findthemiddleindexinarray.java ├── Range sum query- immutable.java └── productofarrayexceptitself.java ├── Hashmap&HashSet ├── find duplicate.java ├── Contains Duplicate.java ├── Majority Element.java ├── Unique number of occurances.java ├── Group Anagrams.java ├── intersection of two arrays.java ├── Intersection of two Array II .java └── longest consecutive sequence.java ├── Two Pointer ├── middle of the linkedlist.java ├── RemoveDuplicateI.java ├── movezeros.java ├── remove element.java ├── find peak element .java ├── containerwithmostwater.java ├── findduplicatenumber.java ├── twosumII.java ├── sqauresofsortedaraay.java ├── searchinsertposition.java ├── validpalindrome.java └── Trapping Rain Water.java ├── kadensAlgorithm ├── maximumsubarray.java └── maximumproductsubarray.java ├── Greedy Algorithm ├── besttimetobuyandsellstock.java ├── max69num.java └── besttimetobuyandsellstock2.java ├── Tree ├── Path Sum.java ├── Binary Tree preorder Traversal.java ├── Binary Tree postorder Traversal.java ├── Binary Tree Level Order Traversal.java ├── Binary Tree Inorder Traversal.java ├── Binary Tree Level order Traversal II.java ├── same tree.java ├── Convert Sorted Array to Binary Search Tree.java └── Convert Sorted List to Binary Search Tree ├── Stack & Queue ├── Daily Temperatures.java ├── Make the String Great.java ├── Remove All Adjacent Duplicates In String.java ├── Clear Digits.java ├── Longest Valid Parentheses.java ├── Remove duplicate Letter.java ├── Implement Stack using Queue.java ├── Implement queue using stack.java ├── Largest Rectangle in Histogram.java ├── BaseBall Game.java ├── Number of student Unable to eat Lunch.java ├── Valid Paranthesis.java ├── Evaluate Reverse Polish Notation.java ├── Time Needed to Buy Tickets.java └── Next Greater Element I.java ├── Divide and Conquer ├── binarysearch.java ├── Searchinrotatedsortedarray.java ├── Kthlargestelementinarray.java ├── firstandlastpositionofelementinsortedarray.java └── convert sorted array into binary search tree.java ├── sliding window ├── minimum size subarray sum .java └── maximum average subarray I .java └── README.md /String/valid Anagram.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Linked List/Singly Linked List/Delete a node from a LL.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void deleteNode(ListNode node) { 3 | node.val = node.next.val; 4 | node.next = node.next.next; 5 | } 6 | } -------------------------------------------------------------------------------- /String/Rotate String.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean rotateString(String s, String goal) { 3 | if(s.length()!=goal.length()) 4 | { 5 | return false; 6 | } 7 | return (s+s).contains(goal); 8 | } 9 | } -------------------------------------------------------------------------------- /Array/singlenumbe.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int index=0; 4 | for(int i =0;i set = new HashSet<>(); 4 | int len = nums.length; 5 | for (int i = 0; i < len; i++) { 6 | if (!set.add(nums[i])) { 7 | return nums[i]; 8 | } 9 | } 10 | 11 | return len; 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /String/Find Words Containing Character.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findWordsContaining(String[] words, char x) { 3 | List ans = new ArrayList<>(); 4 | for (int i = 0; i < words.length; ++i) { 5 | if (words[i].indexOf(x) != -1) { 6 | ans.add(i); 7 | } 8 | } 9 | return ans; 10 | } 11 | } -------------------------------------------------------------------------------- /Hashmap&HashSet/find duplicate.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDuplicate(int[] nums) { 3 | Set set = new HashSet<>(); 4 | int len = nums.length; 5 | for (int i = 0; i < len; i++) { 6 | if (!set.add(nums[i])) { 7 | return nums[i]; 8 | } 9 | } 10 | 11 | return len; 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /Two Pointer/middle of the linkedlist.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode middleNode(ListNode head) { 3 | if(head==null || head.next == null) return head; 4 | ListNode slow=head,fast=head; 5 | while(fast != null && fast.next != null){ 6 | slow = slow.next; 7 | fast = fast.next.next; 8 | } 9 | return slow; 10 | } 11 | } -------------------------------------------------------------------------------- /Array/PalindromeNumber.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(int x) { 3 | if(x<0) 4 | { 5 | return false; 6 | } 7 | int rev =0; 8 | int num =x; 9 | while(num!=0) 10 | { 11 | rev = rev* 10 +num%10; 12 | num = num/10; 13 | } 14 | return (rev ==x); 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /Array/find the highest altitude.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int largestAltitude(int[] gain) { 3 | int currentAltitude =0 ; 4 | int maxaltitude =0; 5 | for(int i=0;iprod2) 8 | { 9 | prod2=prod1; 10 | } 11 | 12 | return prod2; 13 | } 14 | } -------------------------------------------------------------------------------- /Array/RemoveDuplicateI.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | if(nums.length == 0) return 0; 4 | int j=1; 5 | for(int i=1;i='A'&& ch<='Z') 9 | { 10 | ch =(char)(ch+32); 11 | 12 | } 13 | result +=ch; 14 | 15 | } 16 | return result ; 17 | } 18 | } -------------------------------------------------------------------------------- /Array/Contains Duplicate.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public boolean containsDuplicate(int[] nums) { 5 | HashSet set = new HashSet<>(); 6 | 7 | for (int i = 0; i < nums.length; i++) { 8 | if (set.contains(nums[i])) { 9 | return true; 10 | } 11 | set.add(nums[i]); 12 | } 13 | 14 | return false; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Array/besttimetobuyandsellstock2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | int bp=prices[0]; 4 | int maxprofit=0; 5 | for(int i=1;iarr[(i+1)%n]){ 7 | rotate++; 8 | } 9 | } 10 | if(rotate<=1){ 11 | return true; 12 | } 13 | else{ 14 | return false; 15 | } 16 | 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /String/Check If Two String Arrays are Equivalent.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean arrayStringsAreEqual(String[] word1, String[] word2) { 3 | String str1=""; 4 | String str2=""; 5 | for(int i =0;i set = new HashSet<>(); 6 | 7 | for (int i = 0; i < nums.length; i++) { 8 | if (set.contains(nums[i])) { 9 | return true; 10 | } 11 | set.add(nums[i]); 12 | } 13 | 14 | return false; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Array/Majority Element.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public int majorityElement(int[] nums) { 5 | HashMap map = new HashMap<>(); 6 | int n = nums.length; 7 | 8 | for (int i = 0; i < n; i++) { 9 | map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); 10 | 11 | if (map.get(nums[i]) > n / 2) { 12 | return nums[i]; 13 | } 14 | } 15 | 16 | return -1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Hashmap&HashSet/Majority Element.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public int majorityElement(int[] nums) { 5 | HashMap map = new HashMap<>(); 6 | int n = nums.length; 7 | 8 | for (int i = 0; i < n; i++) { 9 | map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); 10 | 11 | if (map.get(nums[i]) > n / 2) { 12 | return nums[i]; 13 | } 14 | } 15 | 16 | return -1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Array/find peak element .java: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | public int findPeakElement(int[] nums) { 5 | int left =0; 6 | int right =nums.length-1; 7 | while(left map = new HashMap<>(); 5 | for (int i = 0; i < arr.length; i++) { 6 | if (map.containsKey(arr[i])) { 7 | map.put(arr[i], map.get(arr[i]) + 1); 8 | } else { 9 | map.put(arr[i], 1); 10 | } 11 | } 12 | 13 | HashSet set = new HashSet<>(map.values()); 14 | return map.size() == set.size(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /String/Find the Index of the First Occurrence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int strStr(String haystack, String needle) { 3 | int a = needle.length(); 4 | int b = haystack.length(); 5 | if (a == 0) { 6 | return 0; 7 | } 8 | if (b < a) 9 | { 10 | return -1; 11 | } 12 | for (int i = 0; i <= b-a; i++) 13 | { 14 | if (haystack.substring(i, i + a).equals(needle)) { 15 | return i; 16 | } 17 | } 18 | return -1; 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Array/findpivotindex.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int pivotIndex(int[] nums) { 3 | int n = nums.length; 4 | int sum= 0; 5 | for(int i =0;i map = new HashMap<>(); 5 | for (int i = 0; i < arr.length; i++) { 6 | if (map.containsKey(arr[i])) { 7 | map.put(arr[i], map.get(arr[i]) + 1); 8 | } else { 9 | map.put(arr[i], 1); 10 | } 11 | } 12 | 13 | HashSet set = new HashSet<>(map.values()); 14 | return map.size() == set.size(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Prefix Sum/findpivotindex.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int pivotIndex(int[] nums) { 3 | int n = nums.length; 4 | int sum= 0; 5 | for(int i =0;i stack = new Stack<>(); 5 | for(int i =0 ;i temperatures[stack.peek()]) 8 | { 9 | int index = stack.pop(); 10 | ans[index]= i-index; 11 | } 12 | stack.push(i); 13 | 14 | } 15 | return ans; 16 | } 17 | } -------------------------------------------------------------------------------- /Two Pointer/containerwithmostwater.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxArea(int[] height) { 3 | int low =0; 4 | int maxarea =0; 5 | int high = height.length-1; 6 | while(low <= high) 7 | { 8 | maxarea = Math.max(maxarea, (high - low) * Math.min(height[low], height[high])); 9 | if(height[low]mid){ 15 | high=mid; 16 | } 17 | else{ 18 | low =mid+1; 19 | } 20 | } 21 | return low; 22 | } 23 | } -------------------------------------------------------------------------------- /Linked List/Singly Linked List/Reverse a Linked list Iterative.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public ListNode reverseIterative(ListNode head) { 3 | if(head == null || head.next==null) 4 | { 5 | return head; 6 | } 7 | ListNode prev = head; 8 | ListNode current = head.next; 9 | 10 | while (current != null) { 11 | ListNode next = current.next; 12 | current.next = prev; 13 | prev = current; 14 | current = next; 15 | } 16 | head.next= null; 17 | return prev; 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /Two Pointer/findduplicatenumber.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDuplicate(int[] nums) { 3 | int low=0; 4 | int high=nums.length-1; 5 | 6 | while(lowmid){ 15 | high=mid; 16 | } 17 | else{ 18 | low =mid+1; 19 | } 20 | } 21 | return low; 22 | } 23 | } -------------------------------------------------------------------------------- /Array/Two Sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | int start =0; 4 | int end =nums.length -1; 5 | while(start target) 13 | { 14 | start++; 15 | } 16 | else 17 | { 18 | end--; 19 | } 20 | } 21 | return new int[]{}; 22 | } 23 | } -------------------------------------------------------------------------------- /Array/binarysearch.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int left = 0; 4 | int right =nums.length-1; 5 | while (left <= right) 6 | { 7 | int mid = left +(right -left )/2; 8 | if(nums[mid]==target) 9 | { 10 | return mid; 11 | } 12 | else if(nums[mid]< target) 13 | { 14 | left = mid +1; 15 | } 16 | else 17 | { 18 | right =mid-1; 19 | } 20 | } 21 | return -1; 22 | } 23 | } -------------------------------------------------------------------------------- /Array/maximumproductsubarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int[] nums) { 3 | int maxprod = nums[0]; 4 | int minprod = nums[0]; 5 | int result = nums[0]; 6 | 7 | for (int i = 1; i < nums.length; i++) { 8 | int temp = maxprod; 9 | 10 | maxprod = Math.max(nums[i], Math.max(maxprod * nums[i], minprod * nums[i])); 11 | minprod = Math.min(nums[i], Math.min(temp * nums[i], minprod * nums[i])); 12 | 13 | result = Math.max(result, maxprod); 14 | } 15 | return result; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Tree/Binary Tree preorder Traversal.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List preorderTraversal(TreeNode root) { 3 | ArrayList ans = new ArrayList<>(); 4 | Stack s = new Stack<>(); 5 | TreeNode curr = root; 6 | 7 | while (curr != null || !s.isEmpty()) { 8 | while (curr != null) { 9 | ans.add(curr.val); 10 | s.push(curr); 11 | curr = curr.left; 12 | } 13 | curr = s.pop(); 14 | 15 | curr = curr.right; 16 | } 17 | 18 | return ans; 19 | } 20 | } -------------------------------------------------------------------------------- /Array/Range sum query- immutable.java: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | private int[] prefix; 3 | 4 | public NumArray(int[] nums) { 5 | int n = nums.length; 6 | prefix = new int [n+1]; 7 | for(int i= 0 ;i< n ; i++) 8 | { 9 | prefix[i +1]=prefix[i]+nums[i]; 10 | } 11 | } 12 | 13 | public int sumRange(int left, int right) { 14 | return prefix[right + 1] - prefix[left]; 15 | } 16 | } 17 | 18 | /** 19 | * Your NumArray object will be instantiated and called as such: 20 | * NumArray obj = new NumArray(nums); 21 | * int param_1 = obj.sumRange(left,right); 22 | */ -------------------------------------------------------------------------------- /Divide and Conquer/binarysearch.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int left = 0; 4 | int right =nums.length-1; 5 | while (left <= right) 6 | { 7 | int mid = left +(right -left )/2; 8 | if(nums[mid]==target) 9 | { 10 | return mid; 11 | } 12 | else if(nums[mid]< target) 13 | { 14 | left = mid +1; 15 | } 16 | else 17 | { 18 | right =mid-1; 19 | } 20 | } 21 | return -1; 22 | } 23 | } -------------------------------------------------------------------------------- /Prefix Sum/Range sum query- immutable.java: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | private int[] prefix; 3 | 4 | public NumArray(int[] nums) { 5 | int n = nums.length; 6 | prefix = new int [n+1]; 7 | for(int i= 0 ;i< n ; i++) 8 | { 9 | prefix[i +1]=prefix[i]+nums[i]; 10 | } 11 | } 12 | 13 | public int sumRange(int left, int right) { 14 | return prefix[right + 1] - prefix[left]; 15 | } 16 | } 17 | 18 | /** 19 | * Your NumArray object will be instantiated and called as such: 20 | * NumArray obj = new NumArray(nums); 21 | * int param_1 = obj.sumRange(left,right); 22 | */ -------------------------------------------------------------------------------- /kadensAlgorithm/maximumproductsubarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int[] nums) { 3 | int maxprod = nums[0]; 4 | int minprod = nums[0]; 5 | int result = nums[0]; 6 | 7 | for (int i = 1; i < nums.length; i++) { 8 | int temp = maxprod; 9 | 10 | maxprod = Math.max(nums[i], Math.max(maxprod * nums[i], minprod * nums[i])); 11 | minprod = Math.min(nums[i], Math.min(temp * nums[i], minprod * nums[i])); 12 | 13 | result = Math.max(result, maxprod); 14 | } 15 | return result; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Array/twosumII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] numbers, int target) { 3 | int start = 0; 4 | int end =numbers.length - 1; 5 | while(start postorderTraversal(TreeNode root) { 3 | ArrayList ans = new ArrayList<>(); 4 | Stack s = new Stack<>(); 5 | TreeNode curr = root; 6 | 7 | while (curr != null || !s.isEmpty()) { 8 | while (curr != null) { 9 | ans.add(curr.val); 10 | s.push(curr); 11 | curr = curr.right; 12 | } 13 | curr = s.pop(); 14 | curr = curr.left; 15 | } 16 | Collections.reverse(ans); 17 | 18 | return ans; 19 | } 20 | } -------------------------------------------------------------------------------- /Two Pointer/twosumII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] numbers, int target) { 3 | int start = 0; 4 | int end =numbers.length - 1; 5 | while(start = Math.abs(nums[high])) { 12 | result[i] = nums[low] * nums[low]; 13 | low++; 14 | } else { 15 | result[i] = nums[high] * nums[high]; 16 | high--; 17 | } 18 | i--; 19 | } 20 | return result; 21 | } 22 | } -------------------------------------------------------------------------------- /String/Longest Substring Without Repeating Characters.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLongestSubstring(String s) { 3 | int left =0; 4 | int maxlen=0; 5 | HashSet window =new HashSet<>(); 6 | for(int right =0;right= Math.abs(nums[high])) { 12 | result[i] = nums[low] * nums[low]; 13 | low++; 14 | } else { 15 | result[i] = nums[high] * nums[high]; 16 | high--; 17 | } 18 | i--; 19 | } 20 | return result; 21 | } 22 | } -------------------------------------------------------------------------------- /Linked List/Singly Linked List/intersection of two linkedlist.java: -------------------------------------------------------------------------------- 1 | ublic class Solution { 2 | public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 3 | ListNode A = headA,B= headB; 4 | while(A!=B) 5 | { 6 | if(A==null) 7 | { 8 | A= headB; 9 | } 10 | else 11 | { 12 | A= A.next; 13 | } 14 | if(B==null) 15 | { 16 | B= headA; 17 | } 18 | else 19 | { 20 | B= B.next; 21 | } 22 | } 23 | return A; 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /String/Group Anagrams.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> groupAnagrams(String[] strs) { 3 | HashMap> map = new HashMap<>(); 4 | for(int i =0;i()); 13 | } 14 | map.get(sorted).add(word); 15 | } 16 | return new ArrayList<>(map.values()); 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /String/FizzBuzz.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List fizzBuzz(int n) { 3 | List ans = new ArrayList<>(); 4 | for(int i =1;i<=n;i++) 5 | { 6 | if(i% 3==0 && i%5==0) 7 | { 8 | ans.add("FizzBuzz"); 9 | } 10 | else if(i%3==0) 11 | { 12 | ans .add("Fizz"); 13 | } 14 | else if(i%5==0) 15 | { 16 | ans.add("Buzz"); 17 | } 18 | else 19 | { 20 | ans.add(Integer.toString(i)); 21 | } 22 | } 23 | return ans; 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Array/minimum size subarray sum .java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSubArrayLen(int target, int[] nums) { 3 | int start =0; 4 | int result =nums.length+1; 5 | int sum =0; 6 | for(int end =0;end=target) 11 | { 12 | result =Math.min(result, end-start +1); 13 | sum -=nums[start]; 14 | start++; 15 | } 16 | } 17 | if (result == nums.length + 1) { 18 | return 0; 19 | } else { 20 | return result; 21 | 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Array/searchinsertposition.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int searchInsert(int[] nums, int target) { 3 | int left=0; 4 | int i=nums.length; 5 | int right=nums.length-1; 6 | while(left<=right) 7 | { 8 | int mid = left+(right-left)/2; 9 | if(nums[mid]==target) 10 | { 11 | return mid; 12 | } 13 | else if(nums[mid]> groupAnagrams(String[] strs) { 3 | HashMap> map = new HashMap<>(); 4 | for(int i =0;i()); 13 | } 14 | map.get(sorted).add(word); 15 | } 16 | return new ArrayList<>(map.values()); 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /String/First Unique Character in a String.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int firstUniqChar(String s) { 3 | for(int i=0;i=target) 11 | { 12 | result =Math.min(result, end-start +1); 13 | sum -=nums[start]; 14 | start++; 15 | } 16 | } 17 | if (result == nums.length + 1) { 18 | return 0; 19 | } else { 20 | return result; 21 | 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /String/Reverse a letter in string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String reverseOnlyLetters(String s) { 3 | char[] c = s.toCharArray(); 4 | int left = 0; 5 | int right = c.length - 1; 6 | 7 | while (left < right) { 8 | if (!Character.isLetter(c[right])) { 9 | right--; 10 | } else if (!Character.isLetter(c[left])) { 11 | left++; 12 | } else { 13 | char temp = c[right]; 14 | c[right] = c[left]; 15 | c[left] = temp; 16 | left++; 17 | right--; 18 | } 19 | } 20 | return new String(c); 21 | } 22 | } -------------------------------------------------------------------------------- /Array/validpalindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(String s) { 3 | int left = 0; 4 | int right = s.length() - 1; 5 | while (left <= right) { 6 | if (!Character.isLetterOrDigit(s.charAt(left))) { 7 | left++; 8 | } else if (!Character.isLetterOrDigit(s.charAt(right))) { 9 | right--; 10 | } else { 11 | if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { 12 | return false; 13 | } 14 | left++; 15 | right--; 16 | } 17 | } 18 | return true; 19 | } 20 | } -------------------------------------------------------------------------------- /Array/productofarrayexceptitself.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] productExceptSelf(int[] nums) { 3 | int n = nums.length; 4 | int[] prefix = new int[n]; 5 | prefix[0]=1; 6 | for(int i= 1 ;i =0 ;i --) 13 | { 14 | suffix[i] =suffix[i+1] * nums[i+1]; 15 | } 16 | 17 | int [] res = new int [n]; 18 | for(int i = 0 ;i st = new Stack<>(); 4 | 5 | for (int i = 0; i < s.length(); i++) { 6 | char c = s.charAt(i); 7 | 8 | if (!st.isEmpty()) { 9 | char top = st.peek(); 10 | if ((c + 32 == top) || (c - 32 == top)) { 11 | st.pop(); 12 | continue; 13 | } 14 | } 15 | st.push(c); 16 | } 17 | String ans = ""; 18 | while (!st.isEmpty()) { 19 | ans = st.pop() + ans; 20 | } 21 | 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /String/valid palindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(String s) { 3 | int left = 0; 4 | int right = s.length() - 1; 5 | while (left <= right) { 6 | if (!Character.isLetterOrDigit(s.charAt(left))) { 7 | left++; 8 | } else if (!Character.isLetterOrDigit(s.charAt(right))) { 9 | right--; 10 | } else { 11 | if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { 12 | return false; 13 | } 14 | left++; 15 | right--; 16 | } 17 | } 18 | return true; 19 | } 20 | } -------------------------------------------------------------------------------- /Two Pointer/validpalindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(String s) { 3 | int left = 0; 4 | int right = s.length() - 1; 5 | while (left <= right) { 6 | if (!Character.isLetterOrDigit(s.charAt(left))) { 7 | left++; 8 | } else if (!Character.isLetterOrDigit(s.charAt(right))) { 9 | right--; 10 | } else { 11 | if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { 12 | return false; 13 | } 14 | left++; 15 | right--; 16 | } 17 | } 18 | return true; 19 | } 20 | } -------------------------------------------------------------------------------- /Prefix Sum/productofarrayexceptitself.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] productExceptSelf(int[] nums) { 3 | int n = nums.length; 4 | int[] prefix = new int[n]; 5 | prefix[0]=1; 6 | for(int i= 1 ;i =0 ;i --) 13 | { 14 | suffix[i] =suffix[i+1] * nums[i+1]; 15 | } 16 | 17 | int [] res = new int [n]; 18 | for(int i = 0 ;i st = new Stack<>(); 4 | for(int i =0 ;i st= new Stack<>(); 4 | for(int i=0;i stack = new Stack<>(); 6 | stack.push(-1); 7 | int maxLength = 0; 8 | 9 | for (int i = 0; i < s.length(); i++) { 10 | if (s.charAt(i) == '(') { 11 | stack.push(i); 12 | } else { 13 | stack.pop(); 14 | if (stack.isEmpty()) { 15 | stack.push(i); 16 | } else { 17 | maxLength = Math.max(maxLength, i - stack.peek()); 18 | } 19 | } 20 | } 21 | return maxLength; 22 | } 23 | } -------------------------------------------------------------------------------- /String/valid word.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isValid(String word) { 3 | if (word.length()<3) 4 | { 5 | return false; 6 | } 7 | int vowel=0; 8 | int cons=0; 9 | for (int i = 0; i < word.length(); i++) { 10 | char c = word.charAt(i); 11 | 12 | if (Character.isLetter(c)) { 13 | if ("aeiouAEIOU".indexOf(c) != -1) 14 | { 15 | vowel++; 16 | } else 17 | { 18 | cons++; 19 | } 20 | } 21 | else if (!Character.isDigit(c)) 22 | { 23 | return false; 24 | } 25 | } 26 | if (vowel < 1 || cons< 1) 27 | { 28 | return false; 29 | } 30 | 31 | return true; 32 | } 33 | } -------------------------------------------------------------------------------- /Array/Missingnumber.java: -------------------------------------------------------------------------------- 1 | // optimal approach 2 | class Solution { 3 | public int MissingNumber(int[] nums) { 4 | int n = nums.length; 5 | int expectedSum = n * (n + 1) / 2; 6 | int actualSum = 0; 7 | for (int num : nums) { 8 | actualSum += num; 9 | } 10 | return expectedSum - actualSum; 11 | } 12 | } 13 | 14 | // Brute force 15 | class Solution { 16 | public int missingNumber(int[] nums) { 17 | Arrays.sort(nums); 18 | int n =nums.length; 19 | if(nums[0]!=0){ 20 | return 0; 21 | } 22 | if (nums[n - 1] != n) return n; 23 | for(int i=1;i st= new Stack<>(); 6 | HashSet k = new HashSet<>(); 7 | for(int i = 0;i set1 = new HashSet<>(); 6 | for (int i = 0; i < nums1.length; i++) { 7 | set1.add(nums1[i]); 8 | } 9 | 10 | HashSet result = new HashSet<>(); 11 | for (int i = 0; i < nums2.length; i++) { 12 | if (set1.contains(nums2[i])) { 13 | result.add(nums2[i]); 14 | } 15 | } 16 | 17 | int[] ans = new int[result.size()]; 18 | 19 | ArrayList list = new ArrayList<>(result); 20 | for (int j = 0; j < list.size(); j++) { 21 | ans[j] = list.get(j); 22 | } 23 | 24 | return ans; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Hashmap&HashSet/intersection of two arrays.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public int[] intersection(int[] nums1, int[] nums2) { 5 | HashSet set1 = new HashSet<>(); 6 | for (int i = 0; i < nums1.length; i++) { 7 | set1.add(nums1[i]); 8 | } 9 | 10 | HashSet result = new HashSet<>(); 11 | for (int i = 0; i < nums2.length; i++) { 12 | if (set1.contains(nums2[i])) { 13 | result.add(nums2[i]); 14 | } 15 | } 16 | 17 | int[] ans = new int[result.size()]; 18 | 19 | ArrayList list = new ArrayList<>(result); 20 | for (int j = 0; j < list.size(); j++) { 21 | ans[j] = list.get(j); 22 | } 23 | 24 | return ans; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Linked List/Singly Linked List/merge two sorted list iterative.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode mergeTwoLists(ListNode list1, ListNode list2) { 3 | ListNode head = new ListNode(0); 4 | ListNode current = head; 5 | while(list1 != null && list2 != null) { 6 | if (list1.val <= list2.val) { 7 | current.next = list1; 8 | list1 = list1.next; 9 | } else { 10 | current.next = list2; 11 | list2 = list2.next; 12 | } 13 | current = current.next; 14 | } 15 | 16 | if (list1 != null) { 17 | current.next = list1; 18 | } else if (list2 != null) { 19 | current.next = list2; 20 | } 21 | 22 | return head.next; 23 | } 24 | } -------------------------------------------------------------------------------- /Array/Find duplicates number in an array.java: -------------------------------------------------------------------------------- 1 | /*class Solution { 2 | public List findDuplicates(int[] nums) { 3 | List duplicates = new ArrayList<>(); 4 | Set set = new HashSet<>(); 5 | int len = nums.length; 6 | for (int i = 0; i < len; i++) { 7 | if (set.contains(nums[i])) { 8 | duplicates.add(nums[i]); 9 | } 10 | set.add(nums[i]); 11 | } 12 | 13 | return duplicates; 14 | 15 | } 16 | }**/ 17 | class Solution { 18 | public List findDuplicates(int[] nums) { 19 | List res=new ArrayList<>(); 20 | Arrays.sort(nums); 21 | for(int i=1;i q; 3 | 4 | public MyStack() { 5 | q = new LinkedList<>(); 6 | 7 | } 8 | 9 | public void push(int x) { 10 | q.add(x); 11 | for(int i =0;i map = new HashMap<>(); 6 | for (int i = 0; i < nums1.length; i++) { 7 | map.put(nums1[i], map.getOrDefault(nums1[i], 0) + 1); // count frequency 8 | } 9 | 10 | List result = new ArrayList<>(); 11 | for (int i = 0; i < nums2.length; i++) { 12 | if (map.containsKey(nums2[i]) && map.get(nums2[i]) > 0) { 13 | result.add(nums2[i]); 14 | map.put(nums2[i], map.get(nums2[i]) - 1); // decrease count 15 | } 16 | } 17 | 18 | int[] ans = new int[result.size()]; 19 | for (int i = 0; i < result.size(); i++) { 20 | ans[i] = result.get(i); 21 | } 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Array/Trapping Rain Water.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int trap(int[] height) { 3 | int n = height.length; 4 | int left = 0, right = n - 1; 5 | int leftMax = 0, rightMax = 0; 6 | int water = 0; 7 | 8 | while (left <= right) { 9 | if (height[left] < height[right]) { 10 | if (height[left] >= leftMax) { 11 | leftMax = height[left]; 12 | } else { 13 | water += leftMax - height[left]; 14 | } 15 | left++; 16 | } else { 17 | if (height[right] >= rightMax) { 18 | rightMax = height[right]; 19 | } else { 20 | water += rightMax - height[right]; 21 | } 22 | right--; 23 | } 24 | } 25 | 26 | return water; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Hashmap&HashSet/Intersection of two Array II .java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public int[] intersect(int[] nums1, int[] nums2) { 5 | HashMap map = new HashMap<>(); 6 | for (int i = 0; i < nums1.length; i++) { 7 | map.put(nums1[i], map.getOrDefault(nums1[i], 0) + 1); // count frequency 8 | } 9 | 10 | List result = new ArrayList<>(); 11 | for (int i = 0; i < nums2.length; i++) { 12 | if (map.containsKey(nums2[i]) && map.get(nums2[i]) > 0) { 13 | result.add(nums2[i]); 14 | map.put(nums2[i], map.get(nums2[i]) - 1); // decrease count 15 | } 16 | } 17 | 18 | int[] ans = new int[result.size()]; 19 | for (int i = 0; i < result.size(); i++) { 20 | ans[i] = result.get(i); 21 | } 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Two Pointer/Trapping Rain Water.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int trap(int[] height) { 3 | int n = height.length; 4 | int left = 0, right = n - 1; 5 | int leftMax = 0, rightMax = 0; 6 | int water = 0; 7 | 8 | while (left <= right) { 9 | if (height[left] < height[right]) { 10 | if (height[left] >= leftMax) { 11 | leftMax = height[left]; 12 | } else { 13 | water += leftMax - height[left]; 14 | } 15 | left++; 16 | } else { 17 | if (height[right] >= rightMax) { 18 | rightMax = height[right]; 19 | } else { 20 | water += rightMax - height[right]; 21 | } 22 | right--; 23 | } 24 | } 25 | 26 | return water; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Linked List/Doubly Linked List/Flatten a Multilevel Doubly Linked List.java: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public int val; 5 | public Node prev; 6 | public Node next; 7 | public Node child; 8 | }; 9 | */ 10 | class Solution { 11 | public Node flatten(Node head) { 12 | if( head == null) return head; 13 | Node temp = head; 14 | while( temp != null) { 15 | if( temp.child == null ) { 16 | temp = temp.next; 17 | continue; 18 | } 19 | Node t = temp.child; 20 | while( t.next != null ) 21 | t = t.next; 22 | t.next = temp.next; 23 | if( temp.next != null ) temp.next.prev = t; 24 | temp.next = temp.child; 25 | temp.child.prev = temp; 26 | temp.child = null; 27 | } 28 | return head; 29 | } 30 | } -------------------------------------------------------------------------------- /Stack & Queue/Implement queue using stack.java: -------------------------------------------------------------------------------- 1 | class MyQueue { 2 | private Stack s ; 3 | 4 | public MyQueue() { 5 | s = new Stack<>(); 6 | } 7 | 8 | public void push(int x) { 9 | if (s.isEmpty()) { 10 | s.push(x); 11 | return; 12 | } 13 | 14 | int temp = s.pop(); 15 | push(x); 16 | s.push(temp); 17 | } 18 | 19 | public int pop() { 20 | return s.pop(); 21 | 22 | } 23 | 24 | public int peek() { 25 | return s.peek(); 26 | } 27 | 28 | public boolean empty() { 29 | return s.isEmpty(); 30 | } 31 | } 32 | 33 | /** 34 | * Your MyQueue object will be instantiated and called as such: 35 | * MyQueue obj = new MyQueue(); 36 | * obj.push(x); 37 | * int param_2 = obj.pop(); 38 | * int param_3 = obj.peek(); 39 | * boolean param_4 = obj.empty(); 40 | */ -------------------------------------------------------------------------------- /Stack & Queue/Largest Rectangle in Histogram.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int largestRectangleArea(int[] heights) { 3 | Stackst = new Stack<>(); 4 | int maxArea =0; 5 | for(int i =0 ;i<=heights.length;i++ ) 6 | { 7 | int currHeight; 8 | if (i < heights.length) { 9 | currHeight = heights[i]; 10 | } else { 11 | currHeight = 0; 12 | } 13 | while (!st.isEmpty() && currHeight < heights[st.peek()]) { 14 | int height = heights[st.pop()]; 15 | int right = i; 16 | int left = st.isEmpty() ? -1 : st.peek(); 17 | int width = right - left - 1; 18 | 19 | maxArea = Math.max(maxArea, height * width); 20 | } 21 | st.push(i); 22 | 23 | } 24 | return maxArea; 25 | } 26 | } -------------------------------------------------------------------------------- /Stack & Queue/BaseBall Game.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int calPoints(String[] operations) { 3 | Stack st = new Stack<>(); 4 | for(int i =0;i q = new LinkedList<>(); 4 | for (int i = 0; i < students.length; i++) { 5 | q.offer(students[i]); 6 | } 7 | 8 | int sandwichIndex = 0; 9 | int moves = 0; 10 | 11 | while (!q.isEmpty()) { 12 | 13 | int front = q.peek(); 14 | int top = sandwiches[sandwichIndex]; 15 | 16 | if (front == top) 17 | { 18 | q.poll(); 19 | sandwichIndex++; 20 | moves = 0; 21 | } else { 22 | q.offer(q.poll()); 23 | moves++; 24 | if (moves == q.size()) 25 | { 26 | return q.size(); 27 | } 28 | } 29 | } 30 | 31 | return 0; 32 | } 33 | } -------------------------------------------------------------------------------- /Array/Kthlargestelementinarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findKthLargest(int[] nums, int k) { 3 | int low =nums[0]; 4 | int high =nums[0]; 5 | for(int i =0 ;ihigh) 12 | { 13 | high = nums[i]; 14 | } 15 | } 16 | while(low <= high) 17 | { 18 | int count =0; 19 | int mid = low +(high-low)/2; 20 | for(int i=0;imid) 23 | { 24 | count ++; 25 | } 26 | } 27 | if(count >= k) 28 | { 29 | low = mid+1; 30 | } 31 | else 32 | { 33 | high = mid-1; 34 | } 35 | } 36 | 37 | return low; 38 | } 39 | } -------------------------------------------------------------------------------- /Tree/Binary Tree Level Order Traversal.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> levelOrder(TreeNode root) { 3 | if(root==null) 4 | { 5 | return new ArrayList<>(); 6 | } 7 | Queue q = new LinkedList<>(); 8 | List> ans = new ArrayList<>(); 9 | q.offer(root); 10 | int curr =0; 11 | while(!q.isEmpty()) 12 | { 13 | int len = q.size(); 14 | ans.add(new ArrayList<>()); 15 | for(int i =0 ;i inorderTraversal(TreeNode root) { 18 | ArrayList ans = new ArrayList<>(); 19 | Stack s = new Stack<>(); 20 | TreeNode curr = root; 21 | 22 | while (curr != null || !s.isEmpty()) { 23 | while (curr != null) { 24 | s.push(curr); 25 | curr = curr.left; 26 | } 27 | curr = s.pop(); 28 | ans.add(curr.val); 29 | curr = curr.right; 30 | } 31 | 32 | return ans; 33 | } 34 | } -------------------------------------------------------------------------------- /Divide and Conquer/Kthlargestelementinarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findKthLargest(int[] nums, int k) { 3 | int low =nums[0]; 4 | int high =nums[0]; 5 | for(int i =0 ;ihigh) 12 | { 13 | high = nums[i]; 14 | } 15 | } 16 | while(low <= high) 17 | { 18 | int count =0; 19 | int mid = low +(high-low)/2; 20 | for(int i=0;imid) 23 | { 24 | count ++; 25 | } 26 | } 27 | if(count >= k) 28 | { 29 | low = mid+1; 30 | } 31 | else 32 | { 33 | high = mid-1; 34 | } 35 | } 36 | 37 | return low; 38 | } 39 | } -------------------------------------------------------------------------------- /String/Valid Paranthesis.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isValid(String s) { 3 | Stack st = new Stack<>(); 4 | for(int i =0; i st = new Stack<>(); 4 | for(int i =0; i maxAvg) { 33 | maxAvg = currentAvg; 34 | } 35 | } 36 | 37 | return maxAvg; 38 | } 39 | 40 | }**/ 41 | -------------------------------------------------------------------------------- /Tree/Binary Tree Level order Traversal II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> levelOrderBottom(TreeNode root) { 3 | if(root==null) 4 | { 5 | return new ArrayList<>(); 6 | } 7 | Queue q = new LinkedList<>(); 8 | List> ans = new ArrayList<>(); 9 | q.offer(root); 10 | int curr =0; 11 | while(!q.isEmpty()) 12 | { 13 | int len = q.size(); 14 | ans.add(new ArrayList<>()); 15 | for(int i =0 ;i maxAvg) { 33 | maxAvg = currentAvg; 34 | } 35 | } 36 | 37 | return maxAvg; 38 | } 39 | 40 | }**/ 41 | -------------------------------------------------------------------------------- /String/Roman to Integer.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int romanToInt(String s) { 3 | int ans = 0, num = 0; 4 | for (int i = s.length() - 1; i >=0; i--) { 5 | switch (s.charAt(i)) { 6 | case 'I': 7 | num = 1; 8 | break; 9 | case 'V': 10 | num = 5; 11 | break; 12 | case 'X': 13 | num = 10; 14 | break; 15 | case 'L': 16 | num = 50; 17 | break; 18 | case 'C': 19 | num = 100; 20 | break; 21 | case 'D': 22 | num = 500; 23 | break; 24 | case 'M': 25 | num = 1000; 26 | break; 27 | } 28 | if (4 * num < ans) 29 | ans -= num; 30 | else 31 | ans += num; 32 | } 33 | return ans; 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /String/Multiply Strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String multiply(String num1, String num2) { 3 | if (num1.equals("0") || num2.equals("0")) return "0"; 4 | 5 | int n = num1.length(), m = num2.length(); 6 | int[] res = new int[n + m]; 7 | 8 | // Multiply each digit like we do on paper 9 | for (int i = n - 1; i >= 0; i--) { 10 | int digit1 = num1.charAt(i) - '0'; 11 | for (int j = m - 1; j >= 0; j--) { 12 | int digit2 = num2.charAt(j) - '0'; 13 | int sum = res[i + j + 1] + digit1 * digit2; 14 | res[i + j + 1] = sum % 10; // unit place 15 | res[i + j] += sum / 10; // carry 16 | } 17 | } 18 | 19 | // Convert array to string, skipping leading zeros 20 | String result = ""; 21 | for (int i = 0; i < res.length; i++) { 22 | if (result.isEmpty() && res[i] == 0) continue; // skip leading zeros 23 | result += res[i]; 24 | } 25 | 26 | return result.isEmpty() ? "0" : result; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Linked List/Singly Linked List/Add two numbers.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 13 | ListNode head = new ListNode(0); 14 | ListNode curr=head; 15 | int carry=0; 16 | while(l1!=null||l2!=null||carry!=0) 17 | { 18 | int sum =carry; 19 | if(l1!=null) 20 | { 21 | sum +=l1.val; 22 | l1=l1.next; 23 | } 24 | if(l2!=null) 25 | { 26 | sum +=l2.val; 27 | l2=l2.next; 28 | } 29 | int digits = sum % 10; 30 | carry = sum/10; 31 | curr.next = new ListNode(digits); 32 | curr=curr.next; 33 | } 34 | return head.next; 35 | 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /Stack & Queue/Evaluate Reverse Polish Notation.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int evalRPN(String[] tokens) { 3 | Stack st = new Stack<>(); 4 | for (int i = 0; i < tokens.length; i++) { 5 | 6 | String t = tokens[i]; 7 | 8 | if (t.equals("+")) { 9 | int b = st.pop(); 10 | int a = st.pop(); 11 | st.push(a + b); 12 | } 13 | else if (t.equals("-")) { 14 | int b = st.pop(); 15 | int a = st.pop(); 16 | st.push(a - b); 17 | } 18 | else if (t.equals("*")) { 19 | int b = st.pop(); 20 | int a = st.pop(); 21 | st.push(a * b); 22 | } 23 | else if (t.equals("/")) { 24 | int b = st.pop(); 25 | int a = st.pop(); 26 | st.push(a / b); 27 | } 28 | else { 29 | st.push(Integer.parseInt(t)); 30 | } 31 | } 32 | 33 | return st.pop(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Array/firstandlastpositionofelementinsortedarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] searchRange(int[] nums, int target) { 3 | int first = -1, last = -1; 4 | int left = 0, right = nums.length - 1; 5 | 6 | while (left <= right) { 7 | int mid = left + (right - left) / 2; 8 | if (nums[mid] == target) { 9 | first = mid; 10 | right = mid - 1; 11 | } else if (nums[mid] < target) { 12 | left = mid + 1; 13 | } else { 14 | right = mid - 1; 15 | } 16 | } 17 | 18 | left = 0; 19 | right = nums.length - 1; 20 | 21 | while (left <= right) { 22 | int mid = left + (right - left) / 2; 23 | if (nums[mid] == target) { 24 | last = mid; 25 | left = mid + 1; 26 | } else if (nums[mid] < target) { 27 | left = mid + 1; 28 | } else { 29 | right = mid - 1; 30 | } 31 | } 32 | 33 | return new int[] { first, last }; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Array/Rotate Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void rotate(int[] nums, int k) { 3 | int n = nums.length; 4 | k = k % n; // To handle cases where k > n 5 | 6 | // Reverse the entire array 7 | int start = 0; 8 | int end = n - 1; 9 | while (start < end) { 10 | int temp = nums[start]; 11 | nums[start] = nums[end]; 12 | nums[end] = temp; 13 | start++; 14 | end--; 15 | } 16 | 17 | // Reverse the first k elements 18 | start = 0; 19 | end = k - 1; 20 | while (start < end) { 21 | int temp = nums[start]; 22 | nums[start] = nums[end]; 23 | nums[end] = temp; 24 | start++; 25 | end--; 26 | } 27 | 28 | // Reverse the remaining n-k elements 29 | start = k; 30 | end = n - 1; 31 | while (start < end) { 32 | int temp = nums[start]; 33 | nums[start] = nums[end]; 34 | nums[end] = temp; 35 | start++; 36 | end--; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Divide and Conquer/firstandlastpositionofelementinsortedarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] searchRange(int[] nums, int target) { 3 | int first = -1, last = -1; 4 | int left = 0, right = nums.length - 1; 5 | 6 | while (left <= right) { 7 | int mid = left + (right - left) / 2; 8 | if (nums[mid] == target) { 9 | first = mid; 10 | right = mid - 1; 11 | } else if (nums[mid] < target) { 12 | left = mid + 1; 13 | } else { 14 | right = mid - 1; 15 | } 16 | } 17 | 18 | left = 0; 19 | right = nums.length - 1; 20 | 21 | while (left <= right) { 22 | int mid = left + (right - left) / 2; 23 | if (nums[mid] == target) { 24 | last = mid; 25 | left = mid + 1; 26 | } else if (nums[mid] < target) { 27 | left = mid + 1; 28 | } else { 29 | right = mid - 1; 30 | } 31 | } 32 | 33 | return new int[] { first, last }; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Linked List/Singly Linked List/Remove nth node from the end of the list.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode removeNthFromEnd(ListNode head, int n) { 13 | if(head ==null ||head.next==null) 14 | { 15 | return null; 16 | } 17 | int size =0; 18 | ListNode current= head; 19 | while(current !=null) 20 | { 21 | current =current.next; 22 | size++; 23 | } 24 | if(n==size) 25 | { 26 | return head.next; 27 | } 28 | int search =size-n; 29 | int i=1; 30 | ListNode prev= head; 31 | 32 | while(i q = new LinkedList<>(); 25 | for (int i = 0; i < tickets.length; i++) { 26 | q.add(i); 27 | } 28 | 29 | int time = 0; 30 | 31 | while (!q.isEmpty()) { 32 | int i = q.poll(); 33 | tickets[i]--; 34 | time++; 35 | 36 | if (tickets[i] > 0) { 37 | q.add(i); 38 | } 39 | 40 | if (i == k && tickets[i] == 0) { 41 | break; 42 | } 43 | } 44 | 45 | return time; 46 | } 47 | } 48 | */ -------------------------------------------------------------------------------- /Tree/same tree.java: -------------------------------------------------------------------------------- 1 | // Recursive approach 2 | 3 | /** 4 | * Definition for a binary tree node. 5 | * public class TreeNode { 6 | * int val; 7 | * TreeNode left; 8 | * TreeNode right; 9 | * TreeNode() {} 10 | * TreeNode(int val) { this.val = val; } 11 | * TreeNode(int val, TreeNode left, TreeNode right) { 12 | * this.val = val; 13 | * this.left = left; 14 | * this.right = right; 15 | * } 16 | * } 17 | */ 18 | class Solution { 19 | public boolean isSameTree(TreeNode p, TreeNode q) { 20 | if(p==null && q== null) 21 | { 22 | return true; 23 | } 24 | if(p==null||q==null) 25 | { 26 | return false; 27 | } 28 | 29 | return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 30 | 31 | } 32 | } 33 | 34 | // iterative approach 35 | 36 | class Solution { 37 | public boolean isSameTree(TreeNode p, TreeNode q) { 38 | 39 | Stack st= new Stack<>(); 40 | st.push(p); 41 | st.push(q); 42 | while(!st.isEmpty()) 43 | { 44 | TreeNode node1= st.pop(); 45 | TreeNode node2= st.pop(); 46 | if(node1 == null && node2 == null) 47 | { 48 | continue; 49 | } 50 | if(node1 == null || node2 == null || node1.val!= node2.val) 51 | { 52 | return false; 53 | } 54 | st.push(node1.left); 55 | st.push(node2.left); 56 | st.push(node1.right); 57 | st.push(node2.right); 58 | 59 | 60 | } 61 | return true; 62 | 63 | } 64 | } -------------------------------------------------------------------------------- /Divide and Conquer/convert sorted array into binary search tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public TreeNode sortedArrayToBST(int[] nums) { 18 | if (nums == null || nums.length == 0) return null; 19 | 20 | int n = nums.length; 21 | TreeNode root = new TreeNode(0); 22 | Stack nodeStack = new Stack<>(); 23 | Stack leftStack = new Stack<>(); 24 | Stack rightStack = new Stack<>(); 25 | 26 | nodeStack.push(root); 27 | leftStack.push(0); 28 | rightStack.push(n - 1); 29 | 30 | while (!nodeStack.isEmpty()) { 31 | TreeNode curr = nodeStack.pop(); 32 | int left = leftStack.pop(); 33 | int right = rightStack.pop(); 34 | 35 | int mid = (left + right) / 2; 36 | curr.val = nums[mid]; 37 | 38 | 39 | if (mid + 1 <= right) { 40 | curr.right = new TreeNode(0); 41 | nodeStack.push(curr.right); 42 | leftStack.push(mid + 1); 43 | rightStack.push(right); 44 | } 45 | 46 | if (left <= mid - 1) { 47 | curr.left = new TreeNode(0); 48 | nodeStack.push(curr.left); 49 | leftStack.push(left); 50 | rightStack.push(mid - 1); 51 | } 52 | } 53 | 54 | return root; 55 | } 56 | } -------------------------------------------------------------------------------- /Stack & Queue/Next Greater Element I.java: -------------------------------------------------------------------------------- 1 | // Brute force approach just uses two loop 2 | 3 | class Solution { 4 | public int[] nextGreaterElement(int[] nums1, int[] nums2) { 5 | int n = nums1.length; 6 | int[] res = new int [n]; 7 | for(int i =0 ;inums1[i]) 17 | { 18 | nextgreater = nums2[k]; 19 | break; 20 | } 21 | } 22 | break; 23 | } 24 | 25 | } 26 | res[i] = nextgreater; 27 | } 28 | return res; 29 | } 30 | } 31 | 32 | // Optimized approach using Monotonic decreasing stack 33 | class Solution { 34 | public int[] nextGreaterElement(int[] nums1, int[] nums2) { 35 | int[] next = new int [nums2.length]; 36 | Stackst = new Stack<>(); 37 | for(int i =nums2.length-1; i>=0;i--) 38 | { 39 | while(!st.isEmpty() && st.peek()<= nums2[i]) 40 | { 41 | st.pop(); 42 | } 43 | next[i] = st.isEmpty()? -1: st.peek(); 44 | st.push(nums2[i]); 45 | } 46 | int[] res = new int[nums1.length]; 47 | for (int i = 0; i < nums1.length; i++) { 48 | for (int j = 0; j < nums2.length; j++) { 49 | if (nums1[i] == nums2[j]) { 50 | res[i] = next[j]; 51 | break; 52 | } 53 | } 54 | } 55 | return res; 56 | } 57 | } 58 | 59 | 60 | //optimized approach using HashMap and Monotonic decreasing stack -------------------------------------------------------------------------------- /Tree/Convert Sorted Array to Binary Search Tree.java: -------------------------------------------------------------------------------- 1 | // Iterative approach 2 | class Solution { 3 | public TreeNode sortedArrayToBST(int[] nums) { 4 | if (nums == null || nums.length == 0) return null; 5 | 6 | int n = nums.length; 7 | TreeNode root = new TreeNode(0); 8 | Stack nodeStack = new Stack<>(); 9 | Stack leftStack = new Stack<>(); 10 | Stack rightStack = new Stack<>(); 11 | 12 | nodeStack.push(root); 13 | leftStack.push(0); 14 | rightStack.push(n - 1); 15 | 16 | while (!nodeStack.isEmpty()) { 17 | TreeNode curr = nodeStack.pop(); 18 | int left = leftStack.pop(); 19 | int right = rightStack.pop(); 20 | 21 | int mid = (left + right) / 2; 22 | curr.val = nums[mid]; 23 | 24 | 25 | if (mid + 1 <= right) { 26 | curr.right = new TreeNode(0); 27 | nodeStack.push(curr.right); 28 | leftStack.push(mid + 1); 29 | rightStack.push(right); 30 | } 31 | 32 | if (left <= mid - 1) { 33 | curr.left = new TreeNode(0); 34 | nodeStack.push(curr.left); 35 | leftStack.push(left); 36 | rightStack.push(mid - 1); 37 | } 38 | } 39 | 40 | return root; 41 | } 42 | } 43 | 44 | 45 | // recursive approach 46 | 47 | 48 | class Solution { 49 | public TreeNode sortedArrayToBST(int[] nums) { 50 | return helper(nums, 0, nums.length - 1); 51 | } 52 | 53 | private TreeNode helper(int[] nums, int left, int right) { 54 | if (left > right) return null; 55 | int mid = (left + right) / 2; 56 | TreeNode root = new TreeNode(nums[mid]); 57 | root.left = helper(nums, left, mid - 1); 58 | root.right = helper(nums, mid + 1, right); 59 | return root; 60 | 61 | } 62 | } -------------------------------------------------------------------------------- /Linked List/Singly Linked List/Palindromic Linked List.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(ListNode head) { 3 | ListNode slow = head; 4 | ListNode fast = head; 5 | 6 | while (fast != null && fast.next != null) { 7 | fast = fast.next.next; 8 | slow = slow.next; 9 | } 10 | 11 | ListNode prev = null; 12 | while (slow != null) { 13 | ListNode temp = slow.next; 14 | slow.next = prev; 15 | prev = slow; 16 | slow = temp; 17 | } 18 | 19 | ListNode first = head; 20 | ListNode second = prev; 21 | 22 | while (second != null) { 23 | if (first.val != second.val) { 24 | return false; 25 | } 26 | first = first.next; 27 | second = second.next; 28 | } 29 | 30 | return true; 31 | } 32 | } 33 | 34 | /** 35 | * Definition for singly-linked list. 36 | * public class ListNode { 37 | * int val; 38 | * ListNode next; 39 | * ListNode() {} 40 | * ListNode(int val) { this.val = val; } 41 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 42 | * } 43 | 44 | class Solution { 45 | 46 | public ListNode getMiddle(ListNode head) { 47 | ListNode fast = head; 48 | ListNode slow = head; 49 | while (fast.next != null && fast.next.next != null) { 50 | fast = fast.next.next; 51 | slow = slow.next; 52 | } 53 | return slow; 54 | } 55 | 56 | 57 | public ListNode reverse(ListNode head) { 58 | ListNode prev = null; 59 | ListNode curr = head; 60 | 61 | while (curr != null) { 62 | ListNode next = curr.next; 63 | curr.next = prev; 64 | prev = curr; 65 | curr = next; 66 | } 67 | return prev; 68 | } 69 | 70 | 71 | 72 | 73 | public boolean isPalindrome(ListNode head) { 74 | if(head == null || head.next == null) { 75 | return true; 76 | } 77 | 78 | ListNode firstHalfEnd = getMiddle(head); 79 | ListNode secondHalfStart = reverse(firstHalfEnd.next); 80 | ListNode firstHalfStart = head; 81 | 82 | while(secondHalfStart != null) { 83 | if(secondHalfStart.val != firstHalfStart.val) { 84 | return false; 85 | } 86 | secondHalfStart = secondHalfStart.next; 87 | firstHalfStart = firstHalfStart.next; 88 | } 89 | 90 | return true; 91 | } 92 | 93 | }*/ -------------------------------------------------------------------------------- /Tree/Convert Sorted List to Binary Search Tree: -------------------------------------------------------------------------------- 1 | // approach 1: convert linkedlist to array 2 | class Solution { 3 | public TreeNode sortedListToBST(ListNode head) { 4 | if (head == null ) 5 | { 6 | return null; 7 | } 8 | List arr = new ArrayList<>(); 9 | while (head != null) { 10 | arr.add(head.val); 11 | head = head.next; 12 | } 13 | int n = arr.size(); 14 | if (n == 0) return null; 15 | 16 | TreeNode root = new TreeNode(0); 17 | Stack nodeStack = new Stack<>(); 18 | Stack leftStack = new Stack<>(); 19 | Stack rightStack = new Stack<>(); 20 | 21 | nodeStack.push(root); 22 | leftStack.push(0); 23 | rightStack.push(n- 1); 24 | 25 | while (!nodeStack.isEmpty()) { 26 | TreeNode curr = nodeStack.pop(); 27 | int left = leftStack.pop(); 28 | int right = rightStack.pop(); 29 | 30 | int mid = (left + right) / 2; 31 | curr.val = arr.get(mid); 32 | 33 | 34 | if (mid + 1 <= right) { 35 | curr.right = new TreeNode(0); 36 | nodeStack.push(curr.right); 37 | leftStack.push(mid + 1); 38 | rightStack.push(right); 39 | } 40 | 41 | if (left <= mid - 1) { 42 | curr.left = new TreeNode(0); 43 | nodeStack.push(curr.left); 44 | leftStack.push(left); 45 | rightStack.push(mid - 1); 46 | } 47 | } 48 | 49 | return root; 50 | } 51 | } 52 | 53 | 54 | // appraoch 2: Recursive 55 | class Solution { 56 | public TreeNode sortedListToBST(ListNode head) { 57 | if(head==null) 58 | { 59 | return null; 60 | } 61 | if(head.next==null) 62 | { 63 | return new TreeNode(head.val); 64 | } 65 | ListNode slow=head,fast=head,slow_Prev=null; 66 | while(fast!=null && fast.next!=null){ 67 | slow_Prev = slow; 68 | slow = slow.next; 69 | fast = fast.next.next; 70 | } 71 | TreeNode root = new TreeNode(slow.val); 72 | slow_Prev.next = null; 73 | root.left = sortedListToBST(head); 74 | root.right = sortedListToBST(slow.next); 75 | return root; 76 | } 77 | } 78 | 79 | 80 | -------------------------------------------------------------------------------- /Array/longest consecutive sequence.java: -------------------------------------------------------------------------------- 1 | 2 | // Brute force approach 3 | 4 | class Solution { 5 | public static boolean linearSearch(int []nums, int num) { 6 | int n = nums.length; //size of array 7 | for (int i = 0; i < n; i++) { 8 | if (nums[i] == num) 9 | return true; 10 | } 11 | return false; 12 | } 13 | public int longestConsecutive(int[] nums) { 14 | if(nums.length == 0) 15 | { 16 | return 0; 17 | } 18 | int n = nums.length; 19 | int longest = 1; 20 | for(int i =0;i set = new HashSet<>(); 76 | for (int i = 0; i < n; i++) { 77 | set.add(nums[i]); 78 | } 79 | 80 | Object[] arr = set.toArray(); 81 | for (int i = 0; i < arr.length; i++) { 82 | int it = (Integer) arr[i]; 83 | if (!set.contains(it - 1)) { 84 | int cnt = 1; 85 | int x = it; 86 | while (set.contains(x + 1)) { 87 | x = x + 1; 88 | cnt = cnt + 1; 89 | } 90 | longest = Math.max(longest, cnt); 91 | } 92 | } 93 | return longest; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /Hashmap&HashSet/longest consecutive sequence.java: -------------------------------------------------------------------------------- 1 | 2 | // Brute force approach 3 | 4 | class Solution { 5 | public static boolean linearSearch(int []nums, int num) { 6 | int n = nums.length; //size of array 7 | for (int i = 0; i < n; i++) { 8 | if (nums[i] == num) 9 | return true; 10 | } 11 | return false; 12 | } 13 | public int longestConsecutive(int[] nums) { 14 | if(nums.length == 0) 15 | { 16 | return 0; 17 | } 18 | int n = nums.length; 19 | int longest = 1; 20 | for(int i =0;i set = new HashSet<>(); 76 | for (int i = 0; i < n; i++) { 77 | set.add(nums[i]); 78 | } 79 | 80 | Object[] arr = set.toArray(); 81 | for (int i = 0; i < arr.length; i++) { 82 | int it = (Integer) arr[i]; 83 | if (!set.contains(it - 1)) { 84 | int cnt = 1; 85 | int x = it; 86 | while (set.contains(x + 1)) { 87 | x = x + 1; 88 | cnt = cnt + 1; 89 | } 90 | longest = Math.max(longest, cnt); 91 | } 92 | } 93 | return longest; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 📘 Data Structures & Algorithms in Java 2 | 3 | Welcome to my **DSA Journey** — a curated notebook of hand-written, optimized solutions to the most popular problems on **LeetCode**, solved using **Java**. 4 | 5 | --- 6 | 7 | ## 🚀 DSA Dashboard 8 | 9 | | Category | Status | 10 | |---------|--------| 11 | | 🧱 Arrays | ![45](https://img.shields.io/badge/Solved-45-blue) | 12 | | 🔤 Strings | ![21](https://img.shields.io/badge/Solved-21-blue) | 13 | | 🔗 Linked List | ![13](https://img.shields.io/badge/Solved-13-blue) | 14 | | 🧵 Stack & Queue | ![15](https://img.shields.io/badge/Solved-15-blue) | 15 | | 🌳 Trees | [9](https://img.shields.io/badge/Solved-9-blue) | 16 | | 🌐 Graphs | ![Soon](https://img.shields.io/badge/Soon-grey) | 17 | | 🔑 HashMap | ![8](https://img.shields.io/badge/Solved-8-blue) | 18 | | ➕ Prefix Sum | ![7](https://img.shields.io/badge/Solved-7-blue) | 19 | | 🪟 Sliding Window | ![3](https://img.shields.io/badge/Solved-3-blue) | 20 | | 🧭 Two Pointer | ![11](https://img.shields.io/badge/Solved-11-blue) | 21 | | ⚡ Greedy | ![3](https://img.shields.io/badge/Solved-3-blue) | 22 | | 🧩 DP | ![Soon](https://img.shields.io/badge/Soon-grey) | 23 | 24 | --- 25 | 26 | 27 | 77 | 78 | 104 | 105 | 159 | 160 |
28 | 29 | ## 🧱 Array (45) 30 | 1. Two Sum 31 | 2. Two Sum II 32 | 3. Remove Duplicates 33 | 4. Remove Element 34 | 5. Squares of a Sorted Array 35 | 6. Container With Most Water 36 | 7. Find Duplicate 37 | 8. Find Peak Element 38 | 9. Search Insert Position 39 | 10. Maximum Subarray 40 | 11. Maximum Product Subarray 41 | 12. Binary Search 42 | 13. Find First & Last Position 43 | 14. Search in Rotated Array 44 | 15. Kth Largest Element 45 | 16. Find Pivot Index 46 | 17. Product of Array Except Self 47 | 18. Find Middle Index 48 | 19. Find Highest Altitude 49 | 20. Range Sum Query 50 | 21. Contains Duplicate 51 | 22. Majority Element 52 | 23. Unique Number of Occurrences 53 | 24. Best Time to Buy and Sell Stock 54 | 25. Best Time to Buy and Sell Stock II 55 | 26. Check if Array Sorted & Rotated 56 | 27. Concatenation of Array 57 | 28. Intersection of Two Arrays 58 | 29. Intersection of Two Arrays II 59 | 30. Longest Consecutive Sequence 60 | 31. Max 69 Number 61 | 32. Maximum Average Subarray 62 | 33. Minimum Size Subarray Sum 63 | 34. Missing Number 64 | 35. Palindrome Number 65 | 36. Running Sum 66 | 37. Single Number 67 | 38. Subarray Sum Equals K 68 | 39. Find Duplicate Number 69 | 40. Rotate Array 70 | 41. Move Zeros 71 | 42. Maximum Product of Three Numbers 72 | 43. Trapping Rain Water 73 | 44. (Add more) 74 | 45. (Add more) 75 | 76 | 79 | 80 | ## 🔤 String (21) 81 | 1. Reverse String 82 | 2. Reverse String II 83 | 3. Reverse Only Letters 84 | 4. Reverse Vowels 85 | 5. Roman to Integer 86 | 6. Valid Parentheses 87 | 7. Valid Palindrome 88 | 8. Valid Anagram 89 | 9. First Occurrence in a String 90 | 10. Is Subsequence 91 | 11. Word Arrays Equivalent 92 | 12. Longest Substring Without Repeating Characters 93 | 13. Longest Common Prefix 94 | 14. First Unique Character 95 | 15. Find the Difference 96 | 16. To Lower Case 97 | 17. FizzBuzz 98 | 18. Words Containing Character 99 | 19. Rotate String 100 | 20. Group Anagrams 101 | 21. Valid Word 102 | 103 | 106 | 107 | ## 🔗 Linked List (13) 108 | 1. Reverse LL (Iterative) 109 | 2. Reverse LL (Recursive) 110 | 3. Middle of Linked List 111 | 4. Merge Two Sorted LL (Recursive) 112 | 5. Merge Two Sorted LL (Iterative) 113 | 6. Delete Node 114 | 7. Intersection of Two Linked Lists 115 | 8. Delete Middle Node 116 | 9. Remove Duplicates (Sorted) 117 | 10. Remove Nth Node from End 118 | 11. Linked List Cycle 119 | 12. Palindrome Linked List 120 | 13. Add Two Numbers 121 | 122 | --- 123 | 124 | ## 🧵 Stack & Queue (15) 125 | 1. Evaluate Reverse Polish Notation 126 | 2. Remove All Adjacent Duplicates In String 127 | 3. Baseball Game 128 | 4. Clear Digits 129 | 5. Valid Parentheses 130 | 6. Next Greater ElementI 131 | 7. Implement Queue Using Stacks 132 | 8. Implement Stack Using Queues 133 | 9. Daily Temperatures 134 | 10. Remove Duplicate Letters 135 | 11. Longest Valid Parentheses 136 | 12. Largest Rectangle in Histogram 137 | 13. Make the String Great 138 | 14. Time Needed to Buy Tickets 139 | 15. Number of Students Unable to Eat Lunch 140 | 141 | 142 | 143 | --- 144 | 145 | ## 🌳 Tree (9) 146 | 1. Same Tree 147 | 2. Preorder Traversal 148 | 3. Postorder Traversal 149 | 4. Level Order 150 | 5. Convert Sorted Array to BST 151 | 6. Convert Sorted List to BST 152 | 7. Inorder Traversal 153 | 8. Path Sum 154 | 9. Level Order II 155 | 156 | 157 | 158 |
161 | 162 | 163 | ## ✨ Why This Repo Exists 164 | To track progress, revise patterns, and build a strong foundation in DSA for FAANG-level interviews. 165 | 166 | --- 167 | --------------------------------------------------------------------------------