I modeled the problem as a directed graph, where each valid pair (i, j) (with i < j) creates an edge if:
- groups[i] != groups[j],
- words[i] and words[j] have equal length,
- and their Hamming distance is 1.
I modeled the problem as a directed graph, where each valid pair (i, j) (with i < j) creates an edge if:
I started by always selecting the first word. Then, I iterated through the list, and for each word, I checked if its group differs from the group of the last selected word. If so, I added it to the result.
class Solution:
def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
I approached this problem by tracking how many letters of each type exist instead of the full string. Each transformation updates the counts: every z adds two a, and all others move forward in the alphabet. After repeating this process t times, the sum of all counts gives the final length.
class Solution:
def lengthAfterTransformations(self, s: str, t: int) -> int:
I iterated through all 3-digit numbers from 100 to 999, ensuring they are even (i.e., last digit is even). For each candidate, I checked if it could be formed using the available digits considering their counts. I used Counter to handle duplicates correctly, ensuring I only used digits as many times as they appear. I stored valid numbers in a set to avoid duplicates and finally returned them sorted.
class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
I iterate through the array while maintaining a counter that tracks consecutive odd numbers. Each time I encounter an odd number, I increment the counter. If the counter reaches 3, I immediately return True. If I encounter an even number, I reset the counter to 0.
class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
I first calculate the current sum and count of zeroes in both arrays. Since we must replace 0's with positive integers ≥ 1, I consider the minimum possible increment for each zero. I then try to equalize the sums by appropriately choosing values for the zeros. If it’s impossible due to a mismatch in fixed parts, I return -1.
class Solution:
def minSum(self, nums1: List[int], nums2: List[int]) -> int: