├── Arranging Coins ├── Average Salary Excluding the Minimum and Maximum Salary ├── Baseball Game ├── Calculate Score After Performing Instructions ├── Check If Digits Are Equal in String After Operations I ├── Check if Array Is Sorted and Rotated ├── Check if Number is a Sum of Powers of Three ├── Clear Digits ├── Climbing Stairs ├── Compare Strings by Frequency of the Smallest Character ├── Contains Duplicate II ├── Count Equal and Divisible Pairs in an Array ├── Count Good Triplets ├── Count Pairs Whose Sum is Less than Target ├── Count Prefixes of a Given String ├── Count Square Sum Triples ├── Count Substrings That Satisfy K-Constraint I ├── Count Symmetric Integers ├── Count Total Number of Colored Cells ├── Count and Say ├── Distribute Candies Among Children I ├── Distribute Candies to People ├── Divide Array Into Equal Pairs ├── Divisible and Non-divisible Sums Difference ├── Find Missing and Repeated Values ├── Find Most Frequent Vowel and Consonant ├── Find Numbers with Even Number of Digits ├── Find Occurrences of an Element in an Array ├── Find Peak Element ├── Find Smallest Letter Greater Than Target ├── Find Special Substring of Length K ├── Find Subarrays With Equal Sum ├── Find and Replace Pattern ├── Find the K-th Character in String Game I ├── Find the Number of Good Pairs I ├── Find the Town Judge ├── Find the Winning Player in Coin Game ├── Finding 3-Digit Even Numbers ├── First Bad Version ├── Flipping an Image ├── Group Anagrams ├── Guess Number Higher or Lower ├── Hash Divided String ├── Intersection of Multiple Arrays ├── Intersection of Two Arrays ├── Kth Missing Positive Number ├── Largest Positive Integer That Exists With Its Negative ├── Left and Right Sum Differences ├── Longest Common Prefix ├── Longest Palindromic Substring ├── Longest Strictly Increasing or Strictly Decreasing Subarray ├── Longest Unequal Adjacent Groups Subsequence I ├── Lowest Common Ancestor of a Binary Search Tree ├── Matrix Diagonal Sum ├── Max Consecutive Ones III ├── Maximum Ascending Subarray Sum ├── Maximum Number of Integers to Choose From a Range I ├── Maximum Product of Three Numbers ├── Maximum Product of Two Digits ├── Maximum Product of Two Elements in an Array ├── Maximum Unique Subarray Sum After Deletion ├── Minimum Cost of Buying Candies With Discount ├── Minimum Deletions for At Most K Distinct Characters ├── Minimum Equal Sum of Two Arrays After Replacing Zeros ├── Minimum Index Sum of Two Lists ├── Minimum Number Game ├── Minimum Number of Moves to Seat Everyone ├── Minimum Number of Operations to Make Elements in Array Distinct ├── Minimum Operations to Exceed Threshold Value II ├── Most Common Word ├── N-th Tribonacci Number ├── Next Greater Element I ├── Next Greater Numerically Balanced Number ├── Number of 1 Bits ├── Number of Boomerangs ├── Number of Equivalent Domino Pairs ├── Number of Steps to Reduce a Number to Zero ├── Number of Subarrays With LCM Equal to K ├── Number of Substrings Containing All Three Characters ├── Palindromic Substrings ├── Partition Array According to Given Pivot ├── Pass the Pillow ├── Relative Ranks ├── Remove All Occurrences of a Substring ├── Reverse Degree of a String ├── Set Mismatch ├── Single Element in a Sorted Array ├── Smallest Index With Digit Sum Equal to Index ├── Smallest Missing Integer Greater Than Sequential Prefix Sum ├── Smallest Palindromic Rearrangement I ├── Sort Array By Parity ├── Sort Colors ├── Split Strings by Separator ├── Split With Minimum Sum ├── Split a String in Balanced Strings ├── Student Attendance Record I ├── Sum of Variable Length Subarrays ├── Third Maximum Number ├── Type of Triangle ├── Uncommon Words from Two Sentences ├── Unique Paths II ├── Unique path ├── Valid Perfect Square └── Zigzag Grid Traversal With Skip /Arranging Coins: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int arrangeCoins(int n) { 3 | long low = 1, high = n, mid = 0; 4 | while (low <= high) { 5 | mid = (low + high) / 2; 6 | long curr = (mid * (mid + 1)) / 2; 7 | if (curr == n) 8 | return (int) mid; 9 | else if (curr < n) 10 | low = mid + 1; 11 | else 12 | high = mid - 1; 13 | } 14 | return (int) high; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Average Salary Excluding the Minimum and Maximum Salary: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double average(int[] salary) { 3 | Arrays.sort(salary); 4 | int sum = 0, n = salary.length - 1; 5 | for (int i = 1; i < n; i++) { 6 | sum += salary[i]; 7 | } 8 | return (double) sum / (n - 1); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Baseball Game: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int calPoints(String[] operations) { 3 | List lst = new ArrayList<>(); 4 | for (int i = 0; i < operations.length; i++) { 5 | char ch = operations[i].charAt(0); 6 | int s = lst.size(); 7 | if (ch == 'C') 8 | lst.remove(s - 1); 9 | else if (ch == 'D') 10 | lst.add(lst.get(s - 1) * 2); 11 | else if (ch == '+') 12 | lst.add(lst.get(s - 1) + lst.get(s - 2)); 13 | else 14 | lst.add(Integer.parseInt(operations[i])); 15 | } 16 | int ans = 0; 17 | for (int i = 0; i < lst.size(); i++) 18 | ans += lst.get(i); 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Calculate Score After Performing Instructions: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long calculateScore(String[] instructions, int[] values) { 3 | int n = instructions.length; 4 | int[] vis = new int[n]; 5 | long score = 0; 6 | for (int i = 0; i < n; i++) { 7 | if (instructions[i].equals("add")) { 8 | if (vis[i] == 0) { 9 | score += values[i]; 10 | vis[i] = 1; 11 | } else 12 | break; 13 | } else { 14 | vis[i] = 1; 15 | i = i + values[i]; 16 | if (i >= n || i < 0 || vis[i] == 1) 17 | break; 18 | i--; 19 | } 20 | } 21 | return score; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Check If Digits Are Equal in String After Operations I: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean hasSameDigits(String s) { 3 | List lst = new ArrayList<>(); 4 | for (char ch : s.toCharArray()) { 5 | lst.add(ch - '0'); 6 | } 7 | while (lst.size() > 2) { 8 | List lst1 = new ArrayList<>(); 9 | for (int i = 1; i < lst.size(); i++) { 10 | lst1.add((lst.get(i - 1) + lst.get(i)) % 10); 11 | } 12 | lst = lst1; 13 | } 14 | return lst.get(0) == lst.get(1); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Check if Array Is Sorted and Rotated: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean check(int[] nums) { 3 | if(nums.length==1) return true; 4 | int c=0; 5 | for(int i=0;inums[i+1]) 7 | { 8 | c++; 9 | } 10 | } 11 | if(nums[0] 0) { 4 | if (n % 3 == 2) 5 | return false; 6 | n /= 3; 7 | } 8 | return true; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Clear Digits: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String clearDigits(String s) { 3 | StringBuilder str=new StringBuilder(); 4 | for(int i=0;i s[j]) 38 | small = s[j]; 39 | } 40 | return freq[small - 'a']; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Contains Duplicate II: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean containsNearbyDuplicate(int[] nums, int k) { 3 | HashMap seen = new HashMap<>(); 4 | for (int i = 0; i < nums.length; i++) { 5 | if (seen.containsKey(nums[i])) { 6 | if (Math.abs(seen.get(nums[i]) - i) <= k) 7 | return true; 8 | else 9 | seen.put(nums[i], i); 10 | } else 11 | seen.put(nums[i], i); 12 | } 13 | return false; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Count Equal and Divisible Pairs in an Array: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countPairs(int[] nums, int k) { 3 | int c = 0; 4 | for (int i = 0; i < nums.length; i++) { 5 | for (int j = i + 1; j < nums.length; j++) { 6 | if (nums[i] == nums[j] && (i * j) % k == 0) 7 | c++; 8 | } 9 | } 10 | return c; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Count Good Triplets: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countGoodTriplets(int[] arr, int a, int b, int c) { 3 | int n = arr.length; 4 | int ans = 0; 5 | for (int i = 0; i < n; i++) { 6 | for (int j = i + 1; j < n; j++) { 7 | if (Math.abs(arr[i] - arr[j]) <= a) { 8 | for (int k = j + 1; k < n; k++) { 9 | if (Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c) 10 | ans++; 11 | } 12 | } 13 | } 14 | } 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Count Pairs Whose Sum is Less than Target: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countPairs(List nums, int target) { 3 | int c=0; 4 | for(int i=0;i 0) { 7 | s = mapper(s); 8 | } 9 | return s; 10 | } 11 | 12 | public String mapper(String s) { 13 | StringBuilder str = new StringBuilder(); 14 | int c = 1; 15 | char ch = s.charAt(0); 16 | for (int i = 1; i < s.length(); i++) { 17 | if (ch == s.charAt(i)) 18 | c++; 19 | else { 20 | str.append(c).append(ch); 21 | ch = s.charAt(i); 22 | c = 1; 23 | } 24 | } 25 | str.append(c).append(ch); 26 | return str.toString(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Distribute Candies Among Children I: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int distributeCandies(int n, int limit) { 3 | int ans = 0; 4 | for (int i = 0; i <= limit; i++) { 5 | for (int j = 0; j <= Math.min(n - i, limit); j++) { 6 | int k = n - i - j; 7 | if (k >= 0 && k <= limit) 8 | ans++; 9 | } 10 | } 11 | return ans; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Distribute Candies to People: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] distributeCandies(int candies, int num_people) { 3 | int ans[] = new int[num_people]; 4 | int i = 1; 5 | while (candies >= 0) { 6 | ans[(i - 1) % num_people] += i <= candies ? i : candies; 7 | candies -= i; 8 | i++; 9 | } 10 | return ans; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Divide Array Into Equal Pairs: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean divideArray(int[] nums) { 3 | int arr[] = new int[501]; 4 | for (int i = 0; i < nums.length; i++) { 5 | arr[nums[i]]++; 6 | } 7 | for (int i = 0; i < nums.length; i++) { 8 | if (arr[nums[i]] % 2 != 0) 9 | return false; 10 | } 11 | return true; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Divisible and Non-divisible Sums Difference: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int differenceOfSums(int n, int m) { 3 | int n1 = 0; 4 | int n2 = 0; 5 | for (int i = 1; i <= n; i++) { 6 | if (i % m == 0) 7 | n1 += i; 8 | else 9 | n2 += i; 10 | } 11 | return n2 - n1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Find Missing and Repeated Values: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] findMissingAndRepeatedValues(int[][] grid) { 3 | int[] arr = new int[2]; 4 | int[] frequency = new int[grid.length * grid.length + 1]; 5 | for (int i = 0; i < grid.length; i++) { 6 | for (int j = 0; j < grid[0].length; j++) { 7 | frequency[grid[i][j]]++; 8 | } 9 | } 10 | for (int k = 1; k < frequency.length; k++) { 11 | if (frequency[k] == 2) 12 | arr[0] = k; 13 | else if (frequency[k] == 0) 14 | arr[1] = k; 15 | } 16 | return arr; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Find Most Frequent Vowel and Consonant: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxFreqSum(String s) { 3 | int vow[] = new int[26]; 4 | int con[] = new int[26]; 5 | for (char i : s.toCharArray()) { 6 | if (i == 'a' || i == 'e' || i == 'o' || i == 'i' || i == 'u') 7 | vow[i - 'a']++; 8 | else 9 | con[i - 'a']++; 10 | } 11 | int v = 0, c = 0; 12 | for (int i = 0; i < 26; i++) { 13 | v = Math.max(v, vow[i]); 14 | c = Math.max(c, con[i]); 15 | } 16 | return v + c; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Find Numbers with Even Number of Digits: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findNumbers(int[] nums) { 3 | int ans=0; 4 | for(int n:nums){ 5 | if((n>=10 && n<100) || (n>=1000 && n<10000) || (n>=100000 && n<1000000)) ans++; 6 | } 7 | return ans; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Find Occurrences of an Element in an Array: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] occurrencesOfElement(int[] nums, int[] queries, int x) { 3 | int[] ans = new int[queries.length]; 4 | int[] occ = new int[nums.length + 1]; 5 | Arrays.fill(ans, -1); 6 | int count = 0; 7 | for (int i = 0, oi = 1; i < nums.length; i++) { 8 | if (nums[i] == x) { 9 | count++; 10 | occ[oi] = i; 11 | oi++; 12 | } 13 | } 14 | if (count == 0) 15 | return ans; 16 | for (int j = 0; j < queries.length; j++) { 17 | if (count < queries[j]) 18 | continue; 19 | else 20 | ans[j] = occ[queries[j]]; 21 | } 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Find Peak Element: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findPeakElement(int[] nums) { 3 | if (nums.length == 1) 4 | return 0; 5 | int first = 0, last = nums.length - 1, mid; 6 | while (first < last) { 7 | mid = (first + last) / 2; 8 | if (mid < nums.length && mid > 0 && nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) 9 | return mid; 10 | if (mid == 0 && nums[mid] > nums[mid + 1]) 11 | return mid; 12 | if (mid == nums.length - 1 && nums[mid] > nums[mid - 1]) 13 | return mid; 14 | if (nums[mid] > nums[mid + 1]) 15 | last = mid; 16 | else 17 | first = mid + 1; 18 | } 19 | return first; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Find Smallest Letter Greater Than Target: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public char nextGreatestLetter(char[] letters, char target) { 3 | if (letters[letters.length - 1] <= target) 4 | return letters[0]; 5 | for (int i = 0; i < letters.length; i++) { 6 | if (target < letters[i]) 7 | return letters[i]; 8 | } 9 | return '0'; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Find Special Substring of Length K: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean hasSpecialSubstring(String s, int k) { 3 | if (s.length() == 1) 4 | return true; 5 | int ans = 1; 6 | char ch = s.charAt(0); 7 | for (int i = 1; i < s.length(); i++) { 8 | if (s.charAt(i) == ch) { 9 | ans++; 10 | } else { 11 | if (ans == k) 12 | return true; 13 | else { 14 | ch = s.charAt(i); 15 | ans = 1; 16 | } 17 | } 18 | } 19 | return (ans == k); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Find Subarrays With Equal Sum: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean findSubarrays(int[] nums) { 3 | int n=nums.length; 4 | HashSet set=new HashSet<>(); 5 | for(int i=1;i findAndReplacePattern(String[] words, String pattern) { 3 | List matchedString = new ArrayList<>(); 4 | int j; 5 | for (int i = 0; i < words.length; i++) { 6 | String word = words[i]; 7 | Map map = new HashMap<>(); 8 | for (j = 0; j < word.length(); j++) { 9 | char original = word.charAt(j); 10 | char replace = pattern.charAt(j); 11 | if (!map.containsKey(original)) { 12 | if (!map.containsValue(replace)) { 13 | map.put(original, replace); 14 | } else 15 | break; 16 | } else { 17 | if (map.get(original) != replace) 18 | break; 19 | } 20 | } 21 | if (j == word.length()) 22 | matchedString.add(words[i]); 23 | } 24 | return matchedString; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Find the K-th Character in String Game I: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public char kthCharacter(int k) { 3 | StringBuilder s = new StringBuilder("a"); 4 | while (k >= s.length()) { 5 | StringBuilder str = new StringBuilder(); 6 | for (int i = 0; i < s.length(); i++) { 7 | str.append((char) (s.charAt(i) + 1)); 8 | } 9 | s.append(str); 10 | } 11 | return s.charAt(k - 1); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Find the Number of Good Pairs I: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfPairs(int[] nums1, int[] nums2, int k) { 3 | int ans=0; 4 | for(int i=0;i list = new ArrayList<>(); 4 | int arr[] = new int[10]; 5 | for (int i : digits) 6 | arr[i]++; 7 | for (int i = 100; i < 999; i += 2) { 8 | int a = i % 10; 9 | int b = (i / 10) % 10; 10 | int c = (i / 10) / 10; 11 | arr[a] -= 1; 12 | arr[b] -= 1; 13 | arr[c] -= 1; 14 | if (arr[a] >= 0 && arr[b] >= 0 && arr[c] >= 0) 15 | list.add(i); 16 | arr[a] += 1; 17 | arr[b] += 1; 18 | arr[c] += 1; 19 | } 20 | int ans[] = new int[list.size()]; 21 | for (int i = 0; i < ans.length; i++) { 22 | ans[i] = list.get(i); 23 | } 24 | return ans; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /First Bad Version: -------------------------------------------------------------------------------- 1 | /* The isBadVersion API is defined in the parent class VersionControl. 2 | boolean isBadVersion(int version); */ 3 | 4 | public class Solution extends VersionControl { 5 | public int firstBadVersion(int n) { 6 | int left = 0, right = n; 7 | while (left < right) { 8 | int mid = left + (right - left) / 2; 9 | if (isBadVersion(mid)) { 10 | right = mid; 11 | } else { 12 | left = mid + 1; 13 | } 14 | } 15 | return right; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Flipping an Image: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] flipAndInvertImage(int[][] image) { 3 | int n = image.length; 4 | int ans[][] = new int[n][n]; 5 | for (int i = 0; i < n; i++) { 6 | for (int j = n - 1, k = 0; j >= 0; j--) { 7 | ans[i][k++] = image[i][j] == 1 ? 0 : 1; 8 | } 9 | } 10 | return ans; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Group Anagrams: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> groupAnagrams(String[] strs) { 3 | Map> map = new HashMap<>(); 4 | for (String s : strs) { 5 | char[] arr = s.toCharArray(); 6 | Arrays.sort(arr); 7 | String sum = new String(arr); 8 | if (!map.containsKey(sum)) { 9 | map.put(sum, new ArrayList<>()); 10 | } 11 | map.get(sum).add(s); 12 | } 13 | return new ArrayList<>(map.values()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Guess Number Higher or Lower: -------------------------------------------------------------------------------- 1 | /** 2 | * Forward declaration of guess API. 3 | * @param num your guess 4 | * @return -1 if num is higher than the picked number 5 | * 1 if num is lower than the picked number 6 | * otherwise return 0 7 | * int guess(int num); 8 | */ 9 | 10 | public class Solution extends GuessGame { 11 | public int guessNumber(int n) { 12 | int left = 1, right = n; 13 | while (left < right) { 14 | int mid = left + (right - left) / 2; 15 | if (guess(mid) == 0) 16 | return mid; 17 | else if (guess(mid) == -1) { 18 | right = mid; 19 | } else 20 | left = mid + 1; 21 | } 22 | return left; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Hash Divided String: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String stringHash(String s, int k) { 3 | StringBuilder res = new StringBuilder(); 4 | int sum = 0; 5 | for (int i = 0; i < s.length(); i++) { 6 | if ((i + 1) % k == 0) { 7 | sum += s.charAt(i) - 'a'; 8 | res.append((char) ('a' + (sum % 26))); 9 | sum = 0; 10 | } else { 11 | sum += s.charAt(i) - 'a'; 12 | } 13 | } 14 | return res.toString(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Intersection of Multiple Arrays: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List intersection(int[][] nums) { 3 | List ans = new ArrayList<>(); 4 | int[] arr = new int[1001]; 5 | int n = nums.length; 6 | for (int i = 0; i < n; i++) { 7 | for (int j = 0; j < nums[i].length; j++) { 8 | arr[nums[i][j]]++; 9 | if (i == n - 1 && arr[nums[i][j]] == n) 10 | ans.add(nums[i][j]); 11 | } 12 | } 13 | Collections.sort(ans); 14 | return ans; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Intersection of Two Arrays: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] intersection(int[] nums1, int[] nums2) { 3 | Set arr = new HashSet<>(); 4 | Arrays.sort(nums1); 5 | Arrays.sort(nums2); 6 | int p1 = 0, p2 = 0; 7 | while (p1 < nums1.length && p2 < nums2.length) { 8 | if (nums1[p1] == nums2[p2]) { 9 | arr.add(nums1[p1]); 10 | p1++; 11 | p2++; 12 | } else if (nums1[p1] > nums2[p2]) { 13 | p2++; 14 | } else { 15 | p1++; 16 | } 17 | 18 | } 19 | return arr.stream().mapToInt(i -> i).toArray(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Kth Missing Positive Number: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findKthPositive(int[] arr, int k) { 3 | int j = 0; 4 | for (int i = 1; i < 3000; i++) { 5 | if (j < arr.length && arr[j] == i) { 6 | j++; 7 | } else if (j > arr.length) 8 | return i + k - 1; 9 | else { 10 | if (k == 0) 11 | return i - 1; 12 | else { 13 | k--; 14 | if (k == 0) 15 | return i; 16 | } 17 | } 18 | } 19 | return -1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Largest Positive Integer That Exists With Its Negative: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMaxK(int[] nums) { 3 | Arrays.sort(nums); 4 | int i=0,j=nums.length-1; 5 | while(inums[j]) i++; 8 | else 9 | j--; 10 | } 11 | return -1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Left and Right Sum Differences: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] leftRightDifference(int[] nums) { 3 | int n = nums.length; 4 | int rs[] = new int[n]; 5 | int ls[] = new int[n]; 6 | for (int i = 1; i < n; i++) { 7 | ls[i] = ls[i - 1] + nums[i - 1]; 8 | } 9 | for (int i = n - 2; i >= 0; i--) { 10 | rs[i] = rs[i + 1] + nums[i + 1]; 11 | } 12 | for (int i = 0; i < n; i++) { 13 | nums[i] = Math.abs(rs[i] - ls[i]); 14 | } 15 | return nums; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Longest Common Prefix: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String longestCommonPrefix(String[] strs) { 3 | Arrays.sort(strs); 4 | String s1=strs[0]; 5 | String s2=strs[strs.length-1]; 6 | int i=0; 7 | while(i= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { 18 | left--; 19 | right++; 20 | } 21 | return s.substring(left + 1, right); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Longest Strictly Increasing or Strictly Decreasing Subarray: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestMonotonicSubarray(int[] nums) { 3 | int maxCount=1; 4 | int incrementCount=1; 5 | int decrementCount=1; 6 | if(nums.length==1) return maxCount; 7 | for(int i=0;inums[i+1]){ 13 | decrementCount++; 14 | incrementCount=1; 15 | } 16 | else { 17 | incrementCount=1; 18 | decrementCount=1; 19 | } 20 | maxCount=Math.max(maxCount,Math.max(incrementCount,decrementCount)); 21 | } 22 | return maxCount; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Longest Unequal Adjacent Groups Subsequence I: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List getLongestSubsequence(String[] words, int[] groups) { 3 | List ans = new ArrayList<>(); 4 | int prev = -1; 5 | for (int i = 0; i < words.length; i++) { 6 | if (groups[i] != prev) { 7 | prev = groups[i]; 8 | ans.add(words[i]); 9 | } 10 | } 11 | return ans; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Lowest Common Ancestor of a Binary Search Tree: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | 11 | class Solution { 12 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 13 | while(root!=null){ 14 | if(root.val>p.val && root.val>q.val) 15 | { 16 | root=root.left; 17 | } 18 | else if(root.val 0) { 10 | if (early == -1) 11 | early = right; 12 | right++; 13 | k--; 14 | } else { 15 | if (early != -1) { 16 | left = early + 1; 17 | right = left; 18 | early = -1; 19 | k = temp; 20 | } else { 21 | right++; 22 | left = right; 23 | } 24 | } 25 | ans = Math.max(ans, right - left); 26 | } 27 | return ans; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Maximum Ascending Subarray Sum: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxAscendingSum(int[] nums) { 3 | int maxSum=0; 4 | for(int i=0;inums[j-1];j++){ 7 | curSubarraySum+=nums[j]; 8 | } 9 | maxSum=Math.max(maxSum,curSubarraySum); 10 | } 11 | return maxSum; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Maximum Number of Integers to Choose From a Range I: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxCount(int[] banned, int n, int maxSum) { 3 | int sum=0,c=0; 4 | Set ban = new HashSet<>(); 5 | for (int num : banned) { 6 | ban.add(num); 7 | } 8 | for(int i=1;i<=n;i++){ 9 | if(ban.contains(i)) continue; 10 | if(sum+i>maxSum) break; 11 | c++; 12 | sum+=i; 13 | } 14 | return c; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Maximum Product of Three Numbers: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximumProduct(int[] nums) { 3 | Arrays.sort(nums); 4 | int n=nums.length; 5 | return Math.max(nums[0]*nums[1]*nums[n-1],nums[n-1]*nums[n-2]*nums[n-3]); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Maximum Product of Two Digits: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int n) { 3 | if (n < 100) 4 | return (n % 10) * (n / 10); 5 | int max[] = new int[11]; 6 | int i = 0; 7 | while (n != 0) { 8 | max[i] = n % 10; 9 | n /= 10; 10 | i++; 11 | } 12 | Arrays.sort(max); 13 | return max[9] * max[10]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Maximum Product of Two Elements in an Array: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int[] nums) { 3 | Arrays.sort(nums); 4 | int n=nums.length-1; 5 | return (nums[n]-1)*(nums[n-1]-1); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Maximum Unique Subarray Sum After Deletion: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSum(int[] nums) { 3 | Arrays.sort(nums); 4 | if (nums[nums.length - 1] <= 0) 5 | return nums[nums.length - 1]; 6 | int ans = 0; 7 | Set set = new HashSet<>(); 8 | for (int i = 0; i < nums.length; i++) { 9 | if (nums[i] > 0) { 10 | if (set.add(nums[i])) 11 | ans += nums[i]; 12 | } 13 | } 14 | return ans; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Minimum Cost of Buying Candies With Discount: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumCost(int[] cost) { 3 | if (cost.length == 1) 4 | return cost[0]; 5 | int ans = 0; 6 | Arrays.sort(cost); 7 | int skip = 1; 8 | for (int i = cost.length - 1; i >= 0; i--) { 9 | if (skip == 3) { 10 | skip = 1; 11 | continue; 12 | } else { 13 | ans += cost[i]; 14 | skip++; 15 | } 16 | } 17 | return ans; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Minimum Deletions for At Most K Distinct Characters: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minDeletion(String s, int k) { 3 | HashMap map = new HashMap<>(); 4 | for (int i = 0; i < s.length(); i++) { 5 | map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1); 6 | } 7 | List list = new ArrayList<>(map.values()); 8 | Collections.sort(list); 9 | int c = 0; 10 | int n = list.size(); 11 | int i = 0; 12 | while (n > k) { 13 | c += list.get(i++); 14 | n -= 1; 15 | } 16 | return c; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Minimum Equal Sum of Two Arrays After Replacing Zeros: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long minSum(int[] nums1, int[] nums2) { 3 | long minsum = 0; 4 | long n1 = 0, n2 = 0; 5 | boolean b1 = false; 6 | boolean b2 = false; 7 | for (int i : nums1) { 8 | if (i == 0) { 9 | n1 += 1; 10 | b1 = true; 11 | } else 12 | n1 += i; 13 | } 14 | for (int i : nums2) { 15 | if (i == 0) { 16 | n2 += 1; 17 | b2 = true; 18 | } else 19 | n2 += i; 20 | } 21 | if (n1 == n2) 22 | return n1; 23 | minsum = Math.max(n1, n2); 24 | return (minsum == n1 && b2 == true || minsum == n2 && b1 == true) ? minsum : -1; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Minimum Index Sum of Two Lists: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String[] findRestaurant(String[] list1, String[] list2) { 3 | HashMap map = new HashMap<>(); 4 | int index = 0; 5 | for (String str : list1) { 6 | map.put(str, index++); 7 | } 8 | List list = new ArrayList<>(); 9 | int max = Integer.MAX_VALUE; 10 | for (int i = 0; i < list2.length; i++) { 11 | if (map.containsKey(list2[i])) { 12 | int sum = map.get(list2[i]) + i; 13 | if (max > sum) { 14 | max = sum; 15 | list.clear(); 16 | list.add(list2[i]); 17 | } else if (max == sum) { 18 | list.add(list2[i]); 19 | } 20 | } 21 | } 22 | return list.toArray(new String[0]); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Minimum Number Game: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] numberGame(int[] nums) { 3 | if (nums.length == 1) 4 | return nums; 5 | Arrays.sort(nums); 6 | int arr[] = new int[nums.length]; 7 | for (int i = 0; i < nums.length - 1; i += 2) { 8 | arr[i] = nums[i + 1]; 9 | arr[i + 1] = nums[i]; 10 | } 11 | nums = null; 12 | return arr; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Minimum Number of Moves to Seat Everyone: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minMovesToSeat(int[] seats, int[] students) { 3 | Arrays.sort(seats); 4 | Arrays.sort(students); 5 | int ans = 0; 6 | for (int i = 0; i < students.length; i++) { 7 | ans += Math.abs(seats[i] - students[i]); 8 | } 9 | return ans; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Minimum Number of Operations to Make Elements in Array Distinct: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumOperations(int[] nums) { 3 | int n = 1, ans = 0; 4 | while (!hasDup(n, nums)) { 5 | n += 3; 6 | ans++; 7 | } 8 | return ans; 9 | } 10 | 11 | public static boolean hasDup(int n, int[] nums) { 12 | int arr[] = new int[101]; 13 | for (int i = n - 1; i < nums.length; i++) { 14 | arr[nums[i]]++; 15 | if (arr[nums[i]] > 1) 16 | return false; 17 | } 18 | return true; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Minimum Operations to Exceed Threshold Value II: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minOperations(int[] nums, int k) { 3 | PriorityQueue pq = new PriorityQueue<>(); // For maintaining in ascending order 4 | for (long i : nums) { 5 | if (i < k) 6 | pq.add(i); // only add elements which are less than k 7 | } 8 | int count = 0; 9 | while (pq.size() >= 2) //The loop runs only when there are two elements in the pq 10 | { 11 | long first = pq.poll(); 12 | long second = pq.poll(); 13 | long num = first * 2 + second; 14 | if (num < k) 15 | pq.add(num); //After operation also checking the elements less than k 16 | count++; 17 | } 18 | return count + pq.size(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Most Common Word: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String mostCommonWord(String paragraph, String[] banned) { 3 | String[] str=paragraph.split("[\\W_]+"); 4 | Set set=new HashSet<>(Arrays.asList(banned)); 5 | HashMap map=new HashMap<>(); 6 | for(int i=0;i map = new HashMap<>(); 5 | for (int i = 0; i < nums2.length; i++) { 6 | map.put(nums2[i], i); 7 | } 8 | for (int i = 0; i < nums1.length; i++) { 9 | int curr = nums1[i]; 10 | int index = map.get(curr); 11 | int max = -1; 12 | for (int j = index + 1; j < nums2.length; j++) { 13 | if (curr < nums2[j]) { 14 | max = nums2[j]; 15 | break; 16 | } 17 | } 18 | ans[i] = max; 19 | } 20 | return ans; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Next Greater Numerically Balanced Number: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int nextBeautifulNumber(int n) { 3 | for (int i = n + 1; i <= n + 1000000; i++) { 4 | int arr[] = new int[10]; 5 | int t = i; 6 | while (t != 0) { 7 | arr[t % 10]++; 8 | t /= 10; 9 | } 10 | int j; 11 | for (j = 0; j < 10; j++) { 12 | if (arr[j] != 0 && arr[j] != j) 13 | break; 14 | } 15 | if (j == 10) 16 | return i; 17 | } 18 | return 0; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Number of 1 Bits: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int hammingWeight(int n) { 3 | int ans=0; 4 | while(n>0){ 5 | ans+=n&1; 6 | n>>=1; 7 | } 8 | 9 | return ans; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Number of Boomerangs: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfBoomerangs(int[][] points) { 3 | int ans = 0; 4 | int n = points.length; 5 | for (int i = 0; i < n; i++) { 6 | HashMap map = new HashMap<>(); 7 | for (int j = 0; j < n; j++) { 8 | int a = points[i][0] - points[j][0]; 9 | int b = points[i][1] - points[j][1]; 10 | int dist = (a * a) + (b * b); 11 | map.put(dist, map.getOrDefault(dist, 0) + 1); 12 | } 13 | for (int count : map.values()) { 14 | if (count > 1) 15 | ans += count * (count - 1); 16 | } 17 | } 18 | return ans; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Number of Equivalent Domino Pairs: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numEquivDominoPairs(int[][] dominoes) { 3 | int arr[] = new int[100]; 4 | int ans = 0; 5 | for (int[] num : dominoes) { 6 | int val = num[0] < num[1] ? num[0] * 10 + num[1] : num[1] * 10 + num[0]; 7 | ans += arr[val]; 8 | arr[val]++; 9 | } 10 | return ans; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Number of Steps to Reduce a Number to Zero: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfSteps(int num) { 3 | if (num == 0) 4 | return 0; 5 | int c = 0; 6 | while (num != 0) { 7 | c++; 8 | if ((num & 1) == 1) 9 | c++; 10 | num >>= 1; 11 | } 12 | return c - 1; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Number of Subarrays With LCM Equal to K: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subarrayLCM(int[] nums, int k) { 3 | int ans = 0; 4 | for (int i = 0; i < nums.length; i++) { 5 | int lcm = nums[i]; 6 | if (lcm == k) 7 | ans++; 8 | for (int j = i + 1; j < nums.length; j++) { 9 | lcm = (lcm * nums[j]) / gcd(lcm, nums[j]); 10 | if (lcm > k) 11 | break; 12 | if (lcm == k) 13 | ans++; 14 | } 15 | } 16 | return ans; 17 | } 18 | 19 | public int gcd(int a, int b) { 20 | return (b != 0) ? gcd(b, a % b) : a; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Number of Substrings Containing All Three Characters: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfSubstrings(String s) { 3 | int count = 0, first = 0, last = 0, length = s.length(); 4 | int arr[] = new int[3]; 5 | while (last < length) { 6 | char curr = s.charAt(last); 7 | arr[curr - 'a']++; 8 | while (arr[0] > 0 && arr[1] > 0 && arr[2] > 0) { 9 | count += length - last; 10 | char leftcurr = s.charAt(first); 11 | arr[leftcurr - 'a']--; 12 | first++; 13 | } 14 | last++; 15 | } 16 | 17 | return count; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Palindromic Substrings: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countSubstrings(String s) { 3 | if (s.length() == 1) 4 | return 1; 5 | int c = 0; 6 | for (int i = 0; i < s.length(); i++) { 7 | c += checker(s, i, i, 0); 8 | c += checker(s, i, i + 1, 0); 9 | } 10 | return c; 11 | } 12 | 13 | public int checker(String s, int left, int right, int c) { 14 | while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { 15 | c++; 16 | left--; 17 | right++; 18 | } 19 | return c; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Partition Array According to Given Pivot: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] pivotArray(int[] nums, int pivot) { 3 | int[] p = new int[nums.length]; 4 | int index = 0, c = 0; 5 | for (int i = 0; i < nums.length; i++) { 6 | if (nums[i] < pivot) 7 | p[index++] = nums[i]; 8 | else if (nums[i] == pivot) 9 | c++; 10 | } 11 | while (c > 0) { 12 | p[index++] = pivot; 13 | c--; 14 | } 15 | for (int i = 0; i < nums.length; i++) { 16 | if (nums[i] > pivot) 17 | p[index++] = nums[i]; 18 | } 19 | return p; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Pass the Pillow: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int passThePillow(int n, int time) { 3 | int full = time / (n - 1); 4 | int extra = time % (n - 1); 5 | if (full % 2 == 0) 6 | return extra + 1; 7 | else 8 | return n - extra; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Relative Ranks: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String[] findRelativeRanks(int[] score) { 3 | HashMap map = new HashMap<>(); 4 | int n = score.length; 5 | int arr[] = Arrays.copyOf(score, n); 6 | Arrays.sort(arr); 7 | for (int i = n - 1; i >= 0; i--) { 8 | map.put(arr[i], n - i); 9 | } 10 | String ans[] = new String[n]; 11 | for (int i = 0; i < n; i++) { 12 | if (map.get(score[i]) == 1) 13 | ans[i] = "Gold Medal"; 14 | else if (map.get(score[i]) == 2) 15 | ans[i] = "Silver Medal"; 16 | else if (map.get(score[i]) == 3) 17 | ans[i] = "Bronze Medal"; 18 | else 19 | ans[i] = String.valueOf(map.get(score[i])); 20 | } 21 | return ans; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Remove All Occurrences of a Substring: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String removeOccurrences(String s, String part) { 3 | StringBuilder sb=new StringBuilder(s); 4 | while(sb.length()>=part.length()){ 5 | int startIndex=sb.indexOf(part); 6 | if(startIndex!=-1) 7 | { 8 | sb.delete(startIndex,startIndex+part.length()); 9 | } 10 | else 11 | { 12 | break; 13 | } 14 | } 15 | return sb.toString(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Reverse Degree of a String: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverseDegree(String s) { 3 | int sum = 0; 4 | for (int i = 0; i < s.length(); i++) { 5 | sum += ('z' - s.charAt(i) + 1) * (i + 1); 6 | } 7 | return sum; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Set Mismatch: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] findErrorNums(int[] nums) { 3 | int arr[]=new int[nums.length+1]; 4 | for(int i:nums) 5 | arr[i]++; 6 | 7 | int ans[]=new int[2]; 8 | for(int i=1;i map = new HashMap<>(); 6 | for (int i = 0; i < s.length(); i++) { 7 | map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1); 8 | } 9 | StringBuilder half = new StringBuilder(); 10 | String middle = ""; 11 | for (char i = 'a'; i <= 'z'; i++) { 12 | int count = map.getOrDefault(i, 0); 13 | if (count % 2 != 0) { 14 | middle = String.valueOf(i); 15 | count--; 16 | } 17 | for (int j = 0; j < count / 2; j++) { 18 | half.append(i); 19 | } 20 | } 21 | String res = half.toString() + middle + half.reverse().toString(); 22 | return res; 23 | } 24 | }©leetcode 25 | -------------------------------------------------------------------------------- /Sort Array By Parity: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] sortArrayByParity(int[] nums) { 3 | if (nums.length == 0 || nums.length == 1) 4 | return nums; 5 | int left = 0, right = nums.length - 1; 6 | while (left <= right) { 7 | while (left <= right && nums[left] % 2 == 0) 8 | left++; // Move left pointer until an odd number is found 9 | while (left <= right && nums[right] % 2 != 0) 10 | right--; // Move right pointer until an even number is found 11 | // Swap only if left < right (ensuring valid positions) 12 | if (left < right) { 13 | int temp = nums[left]; 14 | nums[left] = nums[right]; 15 | nums[right] = temp; 16 | } 17 | } 18 | return nums; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Sort Colors: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void sortColors(int[] nums) { 3 | int c1 = 0, c2 = 0, c3 = 0; 4 | for (int i : nums) { 5 | if (i == 0) 6 | c1++; 7 | else if (i == 1) 8 | c2++; 9 | else 10 | c3++; 11 | } 12 | for (int i = 0; i < nums.length; i++) { 13 | if (c1 != 0) { 14 | nums[i] = 0; 15 | c1--; 16 | } else if (c2 != 0) { 17 | nums[i] = 1; 18 | c2--; 19 | } else { 20 | nums[i] = 2; 21 | c3--; 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Split Strings by Separator: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List splitWordsBySeparator(List words, char separator) { 3 | List ans = new ArrayList<>(); 4 | String reg = "\\" + separator; 5 | for (int i = 0; i < words.size(); i++) { 6 | String arr[] = (words.get(i)).split(reg); 7 | for (String str : arr) { 8 | if (!str.isEmpty()) { 9 | ans.add(str); 10 | } 11 | } 12 | } 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Split With Minimum Sum: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int splitNum(int num) { 3 | // Convert the number to a character array 4 | char[] arr = String.valueOf(num).toCharArray(); 5 | 6 | // Sort the array to get digits in ascending order 7 | Arrays.sort(arr); 8 | 9 | int num1 = 0, num2 = 0, n = arr.length; 10 | 11 | // Distribute digits alternatively into num1 and num2 12 | for (int i = 0, j = 1; i < n; i += 2, j += 2) { 13 | num1 = num1 * 10 + (arr[i] - '0'); // Add digit at even index to num1 14 | if (j < n) 15 | num2 = num2 * 10 + (arr[j] - '0'); // Add digit at odd index to num2 16 | } 17 | 18 | // Return the sum of both numbers 19 | return num1 + num2; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Split a String in Balanced Strings: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int balancedStringSplit(String s) { 3 | int c = 0; 4 | int ans = 0; 5 | for (char ch : s.toCharArray()) { 6 | if (ch == 'R') 7 | c++; 8 | else 9 | c--; 10 | if (c == 0) 11 | ans++; 12 | } 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Student Attendance Record I: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkRecord(String s) { 3 | int c = 0, l = 0; 4 | for (char ch : s.toCharArray()) { 5 | if (ch == 'A') { 6 | c++; 7 | l = 0; 8 | if (c == 2) 9 | return false; 10 | } else if (ch == 'L') { 11 | l++; 12 | if (l == 3) 13 | return false; 14 | } else 15 | l = 0; 16 | 17 | } 18 | return true; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Sum of Variable Length Subarrays: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subarraySum(int[] nums) { 3 | int sum = 0; 4 | int start; 5 | for (int i = 0; i < nums.length; i++) { 6 | start = Math.max(0, i - nums[i]); 7 | for (int j = start; j <= i; j++) { 8 | sum += nums[j]; 9 | } 10 | } 11 | return sum; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Third Maximum Number: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int thirdMax(int[] nums) { 3 | Arrays.sort(nums); 4 | int tm = nums[nums.length - 1]; 5 | int ans = tm; 6 | int j = 0; 7 | for (int i = nums.length - 2; i >= 0; i--) { 8 | if (j < 3 && nums[i] < tm) { 9 | tm = nums[i]; 10 | j++; 11 | if (j == 2) 12 | return tm; 13 | } 14 | } 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Type of Triangle: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String triangleType(int[] nums) { 3 | if ((nums[0] + nums[1]) <= nums[2] || nums[0] + nums[2] <= nums[1] || nums[1] + nums[2] <= nums[0]) 4 | return "none"; 5 | else if (nums[0] == nums[1] && nums[1] == nums[2]) 6 | return "equilateral"; 7 | else if (nums[0] == nums[1] || nums[0] == nums[2] || nums[1] == nums[2]) 8 | return "isosceles"; 9 | else 10 | return "scalene"; 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Uncommon Words from Two Sentences: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String[] uncommonFromSentences(String s1, String s2) { 3 | List ans = new ArrayList<>(); 4 | String[] str1 = s1.split(" "); 5 | String[] str2 = s2.split(" "); 6 | HashMap map = new HashMap<>(); 7 | for (int i = 0; i < str1.length; i++) { 8 | map.put(str1[i], map.getOrDefault(str1[i], 0) + 1); 9 | } 10 | for (int i = 0; i < str2.length; i++) { 11 | map.put(str2[i], map.getOrDefault(str2[i], 0) + 1); 12 | } 13 | for (String key : map.keySet()) { 14 | if (map.get(key) == 1) 15 | ans.add(key); 16 | } 17 | return ans.toArray(new String[0]); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Unique Paths II: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int uniquePathsWithObstacles(int[][] obstacleGrid) { 3 | if(obstacleGrid[0][0]==1){ 4 | return 0; 5 | } 6 | int row=obstacleGrid.length; 7 | int col=obstacleGrid[0].length; 8 | int dp[][]=new int[row][col]; 9 | dp[0][0]=1; 10 | for(int i=1;i zigzagTraversal(int[][] grid) { 3 | List lst = new ArrayList<>(); 4 | for (int i = 0; i < grid.length; i++) { 5 | if (i % 2 == 0) { 6 | for (int j = 0; j < grid[0].length; j += 2) { 7 | lst.add(grid[i][j]); 8 | } 9 | } else { 10 | for (int k = grid[0].length - 1; k >= 0; k--) { 11 | if (k % 2 != 0) 12 | lst.add(grid[i][k]); 13 | } 14 | } 15 | 16 | } 17 | return lst; 18 | } 19 | } 20 | --------------------------------------------------------------------------------