1913. Maximum Product Difference Between Two Pairs

1913. Maximum Product Difference Between Two Pairs The product difference between two pairs (a, b) and (c, d) is defined as (a * b) - (c * d). For example, the product difference between (5, 6) and (2, 7) is (5 * 6) - (2 * 7) = 16. Given an integer array nums, choose four distinct indices w, x, y, and z such that the product difference between pairs (nums[w], nums[x]) and (nums[y], nums[z]) is maximized. ...

June 30, 2021 · 2 min · volyx

1071. Greatest Common Divisor of Strings

1071. Greatest Common Divisor of Strings For two strings s and t, we say “t divides s” if and only if s = t + … + t (t concatenated with itself 1 or more times) Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2. Example 1: Input: str1 = "ABCABC", str2 = "ABC" Output: "ABC" Example 2: Input: str1 = "ABABAB", str2 = "ABAB" Output: "AB" Example 3: Input: str1 = "LEET", str2 = "CODE" Output: "" Example 4: Input: str1 = "ABCDEF", str2 = "ABC" Output: "" Constraints: ...

June 29, 2021 · 1 min · volyx

204. Count Primes

204. Count Primes Count the number of prime numbers less than a non-negative number, n. Example 1: Input: n = 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. Example 2: Input: n = 0 Output: 0 Example 3: Input: n = 1 Output: 0 Constraints: 0 <= n <= 5 * 106 Solution class Solution { public int countPrimes(int n) { boolean[] numbers = new boolean[n + 1]; for (int p = 2; p <= (int) Math.sqrt(n); p++) { if (numbers[p] == false) { for (int j = p + p; j < n; j += p) { numbers[j] = true; } } } int count = 0; for (int i = 2; i < n; i++) { if (numbers[i] == false) { count++; } } return count; } }

June 29, 2021 · 1 min · volyx

83. Remove Duplicates from Sorted List

1625. Lexicographically Smallest String After Applying Operations Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Example 1: Input: head = [1,1,2] Output: [1,2] Example 2: Input: head = [1,1,2,3,3] Output: [1,2,3] Constraints: The number of nodes in the list is in the range [0, 300]. -100 <= Node.val <= 100 The list is guaranteed to be sorted in ascending order. Solution class Solution { String smallest = null; public String findLexSmallestString(String s, int a, int b) { smallest = s; Set<String> set = new HashSet<>(); dfs(s, set, a, b); return smallest; } void dfs(String current, Set<String> set, int a, int b) { if (set.contains(current)) { return; } set.add(current); if (current.compareTo(smallest) < 0) { smallest = current; } dfs(add(current, a), set, a, b); dfs(rotate(current, b), set, a, b); } String add(String s, int a) { StringBuilder sb = new StringBuilder(s); for (int i = 1; i < s.length(); i += 2) { char c = sb.charAt(i); int value = c - '0'; value += a; value = value % 10; sb.deleteCharAt(i); sb.insert(i, value); } return sb.toString(); } String rotate(String s, int b) { int len = s.length(); return s.substring(len - b, len) + s.substring(0, len - b); } }

June 22, 2021 · 2 min · volyx

1624. Largest Substring Between Two Equal Characters

1624. Largest Substring Between Two Equal Characters Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1. A substring is a contiguous sequence of characters within a string. Example 1: Input: s = "aa" Output: 0 Explanation: The optimal substring here is an empty substring between the two 'a's. Example 2: Input: s = "abca" Output: 2 Explanation: The optimal substring here is "bc". Example 3: Input: s = "cbzxy" Output: -1 Explanation: There are no characters that appear twice in s. Example 4: Input: s = "cabbac" Output: 4 Explanation: The optimal substring here is "abba". Other non-optimal substrings include "bb" and "". Constraints: ...

June 20, 2021 · 1 min · volyx

206. Reverse Linked List

206. Reverse Linked List Given the head of a singly linked list, reverse the list, and return the reversed list. Example 1: Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] Example 2: Input: head = [1,2] Output: [2,1] Example 3: Input: head = [] Output: [] Constraints: The number of nodes in the list is the range [0, 5000]. -5000 <= Node.val <= 5000 Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? ...

June 17, 2021 · 1 min · volyx

237. Delete Node in a Linked List

237. Delete Node in a Linked List Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly. It is guaranteed that the node to be deleted is not a tail node in the list. Example 1: Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function. ...

June 16, 2021 · 2 min · volyx

680. Valid Palindrome II

680. Valid Palindrome II Given a string s, return true if the s can be palindrome after deleting at most one character from it. Example 1: Input: s = "aba" Output: true Example 2: Input: s = "abca" Output: true Explanation: You could delete the character 'c'. Example 3: Input: s = "abc" Output: false Constraints: 1 <= s.length <= 105 s consists of lowercase English letters. Solution class Solution { public boolean validPalindrome(String s) { return palindrom(s, 0); } boolean palindrom(String s, int count) { int i = 0; int n = s.length(); while (i < n / 2) { char c1 = s.charAt(i); char c2 = s.charAt(n - i - 1); if (c1 == c2) { i++; continue; } else { if (count == 0) { StringBuilder sb1 = new StringBuilder(s); sb1.deleteCharAt(i); boolean res1 = palindrom(sb1.toString(), 1); if (res1) return true; StringBuilder sb2 = new StringBuilder(s); sb2.deleteCharAt(n - i - 1); return palindrom(sb2.toString(), 1); } return false; } } return true; } } Solution 2021-11-21 class Solution { public boolean validPalindrome(String s) { return validatePalindrom(s, 1, 0, s.length() - 1); } boolean validatePalindrom(String s, int deleted, int lo, int hi) { while (lo < hi) { if (s.charAt(lo) == s.charAt(hi)) { lo++; hi--; } else { if (deleted == 0) { return false; } return validatePalindrom(s, 0, lo + 1, hi) || validatePalindrom(s, 0, lo, hi - 1); } } return true; } } Solution 2022-01-22 class Solution { public boolean validPalindrome(String s) { char[] symbols = s.toCharArray(); return validPalindrome(symbols, 0, s.length() - 1, 1); } boolean validPalindrome(char[] symbols, int i, int j, int count) { while (i < j) { if (symbols[i] == symbols[j]) { i++; j--; continue; } else { if (count > 0) { return validPalindrome(symbols, i, j - 1, 0) || validPalindrome(symbols, i + 1, j, 0); } else { return false; } } } return true; } }

June 15, 2021 · 2 min · volyx

67. Add Binary

67. Add Binary Given two binary strings a and b, return their sum as a binary string. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010", b = "1011" Output: "10101" Constraints: 1 <= a.length, b.length <= 104 a and b consist only of ‘0’ or ‘1’ characters. Each string does not contain leading zeros except for the zero itself. Solution class Solution { public String addBinary(String a, String b) { int i = a.length() - 1; int j = b.length() - 1; int carry = 0; StringBuilder sb = new StringBuilder(); while (i >= 0 || j >= 0) { int c1 = (i >= 0) ? a.charAt(i) - '0': 0; int c2 = (j >= 0) ? b.charAt(j) - '0': 0; int c3 = c1 + c2 + carry; if (carry > 0) { carry = 0; } if (c3 == 3) { carry = 1; sb.insert(0, '1'); } else if (c3 == 2) { carry = 1; sb.insert(0, '0'); } else if (c3 == 1) { sb.insert(0, '1'); } else { sb.insert(0, '0'); } i--; j--; } if (carry > 0) { sb.insert(0, '1'); } return sb.toString(); } public String addBinary2(String a, String b) { Integer i1 = Integer.parseInt(a, 2); Integer i2 = Integer.parseInt(b, 2); return Integer.toBinaryString(i1 + i2); } }

June 14, 2021 · 2 min · volyx

1897. Redistribute Characters to Make All Strings Equal

1897. Redistribute Characters to Make All Strings Equal You are given an array of strings words (0-indexed). In one operation, pick two distinct indices i and j, where words[i] is a non-empty string, and move any character from words[i] to any position in words[j]. Return true if you can make every string in words equal using any number of operations, and false otherwise. Example 1: Input: words = ["abc","aabc","bc"] Output: true Explanation: Move the first 'a' in words[1] to the front of words[2], to make words[1] = "abc" and words[2] = "abc". All the strings are now equal to "abc", so return true. Example 2: Input: words = ["ab","a"] Output: false Explanation: It is impossible to make all the strings equal using the operation. Constraints: ...

June 13, 2021 · 1 min · volyx