Posts

2342. Max Sum of a Pair With Equal Sum of Digits

You are given a  0-indexed  array  nums  consisting of  positive  integers. You can choose two indices  i  and  j , such that  i != j , and the sum of digits of the number  nums[i]  is equal to that of  nums[j] . Return  the **maximum  value of  nums[i] + nums[j]  that you can obtain over all possible indices  i  and  j  that satisfy the conditions.**    Example 1: Input: nums = [18,43,36,13,7] Output: 54 Explanation: The pairs (i, j) that satisfy the conditions are: - (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54. - (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50. So the maximum sum that we can obtain is 54. Example 2: Input: nums = [10,12,19,14] Output: -1 Explanation: There are no two numbers that satisfy the conditions, so we return -1.    Constraints: 1 <= nums.length <= 105 1 <...

1910. Remove All Occurrences of a Substring

Given two strings  s  and  part , perform the following operation on  s  until  all  occurrences of the substring  part  are removed: Find the  leftmost  occurrence of the substring  part  and  remove  it from  s . Return  s  after removing all occurrences of  part . A  substring  is a contiguous sequence of characters in a string. Example 1: Input:  s = “daabcbaabcbc”, part = “abc” Output:  “dab” Explanation:  The following operations are done: s = “da abc baabcbc”, remove “abc” starting at index 2, so s = “dabaabcbc”. s = “daba abc bc”, remove “abc” starting at index 4, so s = “dababc”. s = “dab abc ”, remove “abc” starting at index 3, so s = “dab”. Now s has no occurrences of “abc”. Example 2: Input:  s = “axxxxyyyyb”, part = “xy” Output:  “ab” Explanation:  The following operations are done: s = “axxx xy yyyb”, remove “xy” starting at index 4 so s = ...

3174. Clear Digits

You are given a string  s . Your task is to remove  all  digits by doing this operation repeatedly: Delete the  first  digit and the  closest   non-digit  character to its  left . Return the resulting string after removing all digits.   Example 1: Input:   s = "abc" Output:   "abc" Explanation: There is no digit in the string. Example 2: Input:   s = "cb34" Output:   "" Explanation: First, we apply the operation on  s[2] , and  s  becomes  "c4" . Then we apply the operation on  s[1] , and  s  becomes  "" .   Constraints: 1 <= s.length <= 100 s  consists only of lowercase English letters and digits. The input is generated such that it is possible to delete all digits. Solution 1: Stack + Simulation JAVA class Solution { public String clearDigits ( String s ) { StringBuilder stk = new StringBuilder (); for ( char c : s . toCharA...

2364 - Count Number of Bad Pairs

You are given a  0-indexed  integer array  nums . A pair of indices  (i, j)  is a  bad pair  if  i < j  and  j - i != nums[j] - nums[i] . Return  the total number of  bad pairs  in  nums .   Example 1: Input: nums = [4,1,3,3] Output: 5 Explanation: The pair (0, 1) is a bad pair since 1 - 0 != 1 - 4. The pair (0, 2) is a bad pair since 2 - 0 != 3 - 4, 2 != -1. The pair (0, 3) is a bad pair since 3 - 0 != 3 - 4, 3 != -1. The pair (1, 2) is a bad pair since 2 - 1 != 3 - 1, 1 != 2. The pair (2, 3) is a bad pair since 3 - 2 != 3 - 3, 1 != 0. There are a total of 5 bad pairs, so we return 5. Example 2: Input: nums = [1,2,3,4,5] Output: 0 Explanation: There are no bad pairs.   Constraints: 1 <= nums.length <= 10 5 1 <= nums[i] <= 10 9 JAVA class Solution { public long countBadPairs ( int [] nums ) { Map < Integer , Integer > cnt = new HashMap <>(); ...