Find Common Characters
Description:
Given a string array words
, return an array of all characters that show up in all strings within the words
(including duplicates). You may return the answer in any order.
Solution:
Maximize Sum Of Array After K Negations
Description: Given an integer array nums and an integer k, modify the array in the following way. Return
Solution:
Partition Array Into Three Parts With Equal Sum
Description:
Given an array of integers arr
, return true
if we can partition the array into three non-empty parts with equal sums.
Formally, we can partition the array if we can find indexes i + 1 < j
with (arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1])
Solution:
Using prefix sum
First we have to find if total sum divisible 3 (because we have to find 3 equal parts)
Second, we have to find first part which equal to target ( where target = total/3)
Then we keep find first 2 equal parts ( we do not need to check third one) while i+1 < j
If we found first 2 equal parts ⇒ return true
if not ⇒ return false;
Binary Prefix Divisible By 5
Description:
You are given a binary array nums
(0-indexed).
We define xi
as the number whose binary representation is the subarray nums[0..i]
(from most-significant-bit to least-significant-bit).
nums = [1,0,1]
, then x0 = 1
, x1 = 2
, and x2 = 5
.Return an array of booleans answer
where answer[i]
is true
if xi
is divisible by 5
.
Solution:
Using Brute-force
num = (num * 2 + nums[i]) % 5;
This means take the current number, shift it left by 1 bit, add the new incoming bit, and keep only its remainder modulo 5.
Because if we using sum ⇒ it might fail because the sum might be really big
Remove Outermost Parentheses
Description: A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation.
Solution 1: use count variable to check:
empty
empty
empty
emptu
empty
empty
Sum of Root To Leaf Binary Numbers
Description:
You are given the root
of a binary tree where each node has a value 0
or 1
. Each root-to-leaf path represents a binary number starting with the most significant bit.
0 -> 1 -> 1 -> 0 -> 1
, then this could represent 01101
in binary, which is 13
.For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of these numbers.
The test cases are generated so that the answer fits in a 32-bits integer.
Solution:
Valid Boomerang
Description: A boomerang is a set of three points that are all distinct and not in a straight line. Solution: use formula to find Area of triangle. If area = 0 ⇒ return false ,otherwise, return true.
Greatest Common Divisor of Strings
Description:
For two strings s
and t
, we say "t
divides s
" if and only if s = t + t + t + ... + t + t
(i.e., t
is concatenated with itself one or more times).
Given two strings str1
and str2
, return the largest string x
such that x
divides both str1
and str2
.
Solution:
str1 + str2 == str2 + str1
. This ensures both strings follow the same repeating pattern.gcd(len1, len2)
to get the length of the repeating substring.str1.substring(0, gcdLength)
.Occurrences After Bigram
Description:
Given two strings first
and second
, consider occurrences in some text of the form "first second third"
, where second
comes immediately after first
, and third
comes immediately after second
.
Return an array of all the words third
for each occurrence of "first second third"
.
Solution:
1103. Distribute Candies to People
Description:
We distribute some number of candies
, to a row of n = num_people
people in the following way:
We then give 1 candy to the first person, 2 candies to the second person, and so on until we give n
candies to the last person.
Then, we go back to the start of the row, giving n + 1
candies to the first person, n + 2
candies to the second person, and so on until we give 2 * n
candies to the last person.
This process repeats (with us giving one more candy each time, and moving to the start of the row after we reach the end) until we run out of candies. The last person will receive all of our remaining candies (not necessarily one more than the previous gift).
Return an array (of length num_people
and sum candies
) that represents the final distribution of candies.
Solution:
Using brute-force ( using modulo to make circular array)
Day of the Year
Description:
Given a string date
representing a Gregorian calendar date formatted as YYYY-MM-DD
, return the day number of the year.
Solution:
Create calendar array with 12 months
if leap year count++;
Leap year is
(year % 400) or (year % 100 ≠ 0 and year % 4)
⇒ Return count;