1768. Merge Strings Alternately
- Initialize two pointers
i
andj
forword1
andword2
. - Use a loop to append characters alternately from
word1
andword2
. - Continue appending from the remaining portion of the longer string after the shorter one is exhausted.
- Return the result string.
1071. Greatest Common Divisor of Strings
- Check if
str1 + str2 == str2 + str1
. If false, return an empty string. - If true, the GCD of string lengths will give the length of the greatest common divisor string.
- Use
gcd(len(str1), len(str2))
to find the length of the common divisor. - Return the substring from either string up to the GCD length.
1431. Kids With the Greatest Number of Candies
- Find the maximum number of candies among all children.
- Iterate over each child's candies and check if adding the
extraCandies
to their current count would make them greater than or equal to the maximum. - Store the result for each child as a boolean value in a list.
- Return the result list.
605. Can Place Flowers
- Iterate through the flowerbed array.
- For each empty plot (0), check if the previous and next plots are also empty or non-existent (handle boundaries).
- If a flower can be planted at the current plot, plant it and decrement
n
. - Return true if all
n
flowers can be planted, otherwise return false.
345. Reverse Vowels of a String
- Create a set of vowels for quick lookup.
- Use two pointers:
left
starting from the beginning andright
from the end. - Move the pointers inward until both point to vowels, then swap them.
- Continue moving until the pointers cross.
- Return the modified string.
151. Reverse Words in a String
- Split the string by spaces to extract words, ignoring any extra spaces.
- Reverse the list of words.
- Join the words back together with a single space between each word.
- Return the resulting string.
238. Product of Array Except Self
- Initialize two arrays:
left
andright
to store the product of all elements to the left and right of each index. - First pass: Fill the
left
array whereleft[i]
is the product of all elements beforei
. - Second pass: Fill the
right
array whereright[i]
is the product of all elements afteri
. - Multiply
left[i]
andright[i]
to get the final result for each index. - Return the resulting product array.
334. Increasing Triplet Subsequence
- Initialize two variables
first
andsecond
to represent the smallest and middle values. - Traverse the array:
- If the current element is smaller than
first
, updatefirst
. - If the current element is greater than
first
but smaller thansecond
, updatesecond
. - If you find a number greater than both
first
andsecond
, return true.
- If the current element is smaller than
- If no such triplet is found, return false.
443. String Compression
- Use a pointer to track the current position in the string.
- For each group of consecutive repeating characters, replace them with the character followed by its count (if more than 1).
- Move to the next group and repeat.
- Return the length of the modified array.
283. Move Zeroes
- Use a pointer to track the position for placing the next non-zero element.
- Traverse the array and move each non-zero element to the front, maintaining their relative order.
- Once all non-zero elements are moved, fill the remaining positions with zeroes.
- Return the modified array.
392. Is Subsequence
- Use two pointers: one for
s
and one fort
. - Traverse
t
, and for each character int
, check if it matches the current character ins
. - If a match is found, move the pointer in
s
to the next character. - If all characters in
s
are found int
in order, return true. - Return false if not all characters in
s
are found.
11. Container With Most Water
- Initialize two pointers: one at the beginning and one at the end of the array.
- Calculate the area between the two lines pointed by the pointers, using the smaller height.
- Move the pointer pointing to the shorter line inward to try and increase the area.
- Continue until the pointers meet.
- Return the maximum area found.
1679. Max Number of K-Sum Pairs
- Sort the array to allow two-pointer traversal.
- Initialize two pointers: one at the start and one at the end.
- Check the sum of the elements at the two pointers:
- If it equals
k
, count the pair and move both pointers inward. - If the sum is less than
k
, move the left pointer inward. - If the sum is greater than
k
, move the right pointer inward.
- If it equals
- Continue until the two pointers meet, then return the count of pairs.
643. Maximum Average Subarray I
- Calculate the sum of the first
k
elements to initialize the window. - Slide the window one element at a time by subtracting the first element of the window and adding the next element.
- Keep track of the maximum sum encountered during the sliding process.
- Return the maximum average by dividing the maximum sum by
k
.
1456. Maximum Number of Vowels in a Substring of Given Length
- Initialize a sliding window of size
k
to count the vowels in the first window. - Slide the window across the string:
- Add a vowel if it enters the window, and subtract one if it leaves.
- Keep track of the maximum number of vowels encountered in any window.
- Return the maximum count.
1004. Max Consecutive Ones III
- Use a sliding window to track the number of
0
s in the current window. - Expand the window by adding elements until the number of
0
s exceedsk
. - When there are more than
k
0
s, shrink the window from the left. - Keep track of the maximum length of the window with at most
k
0
s. - Return the maximum window length.
1493. Longest Subarray of 1's After Deleting One Element
- Use a sliding window to maintain the number of
0
s in the current window. - Expand the window by adding elements until the window contains more than one
0
. - When there are more than one
0
s, shrink the window from the left. - Track the maximum subarray length with at most one
0
. - Return the maximum length.
1732. Find the Highest Altitude
- Initialize the current altitude to 0.
- Traverse through the gain array, adding each gain value to the current altitude.
- Track the maximum altitude encountered during the traversal.
- Return the maximum altitude.
724. Find Pivot Index
- Calculate the total sum of the array.
- Iterate through the array:
- For each element, calculate the right sum as
totalSum - leftSum - nums[i]
. - If
leftSum == rightSum
, return the current index. - Update the
leftSum
by adding the current element.
- For each element, calculate the right sum as
- If no pivot index is found, return -1.