Description:
You are given a string s
consisting of n
characters which are either 'X'
or 'O'
.
A move is defined as selecting three consecutive characters of s
and converting them to 'O'
. Note that if a move is applied to the character 'O'
, it will stay the same.
Return the minimum number of moves required so that all the characters of s
are converted to 'O'
.
Solution:
Every time we hit ‘X’ we will move to next 3
Find Target Indices After Sorting Array
Description:
You are given a 0-indexed integer array nums
and a target element target
.
A target index is an index i
such that nums[i] == target
.
Return a list of the target indices of nums
after sorting nums
in non-decreasing order. If there are no target indices, return an empty list. The returned list must be sorted in increasing order.
Solution:
Using 3 loops( worst)
We using 3 loops to create number which is unique
We sort list increasing order
We convert back to char array
Find Subsequence of Length K With the Largest Sum
Description:
You are given an integer array nums
and an integer k
. You want to find a subsequence of nums
of length k
that has the largest sum.
Return any such subsequence as an integer array of length k
.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Solution:
Using custom Priority Queue<Pair>
We create Pair class with key and index
We find k largest numbers
Convert into List to order by increasing index
Convert back to int[] result
A Number After a Double Reversal
Description:
Reversing an integer means to reverse all its digits.
2021
gives 1202
. Reversing 12300
gives 321
as the leading zeros are not retained.Given an integer num
, reverse num
to get reversed1
, then reverse reversed1
to get reversed2
. Return true
if reversed2
equals num
. Otherwise return false
.
Solution:
Using normal approach
Because when
Check if All A's Appears Before All B's
Description:
Given a string s
consisting of only the characters 'a'
and 'b'
, return true
if every 'a'
appears before every 'b'
in the string. Otherwise, return false
.
Solution:
Description:
Given an integer array nums
, return the number of elements that have both a strictly smaller and a strictly greater element appear in nums
.
Solution:
Find min and max
Compare if the number > min and number < max ( ⇒ so every number will have at least 2 strict number ) ⇒ count++;
⇒ Return count; ( Using normal way faster than using stream)
Minimum Sum of Four Digit Number After Splitting Digits
Description:
You are given a positive integer num
consisting of exactly four digits. Split num
into two new integers new1
and new2
by using the digits found in num
. Leading zeros are allowed in new1
and new2
, and all the digits found in num
must be used.
num = 2932
, you have the following digits: two 2
's, one 9
and one 3
. Some of the possible pairs [new1, new2]
are [22, 93]
, [23, 92]
, [223, 9]
and [2, 329]
.Return the minimum possible sum of new1
and new2
.
Solution:
Using greedy algorithm
We always pick the next smallest to build the smallest sum
[new1,new2] ⇒ always pick the pick smallest one for new1
Count Operations to Obtain Zero
Description:
You are given two non-negative integers num1
and num2
.
In one operation, if num1 >= num2
, you must subtract num2
from num1
, otherwise subtract num1
from num2
.
num1 = 5
and num2 = 4
, subtract num2
from num1
, thus obtaining num1 = 1
and num2 = 4
. However, if num1 = 4
and num2 = 5
, after one operation, num1 = 4
and num2 = 1
.Return the number of operations required to make either num1 = 0
or num2 = 0
.
Solution:
Using normal approach
Using Euclidean(best) - like GCD simulation
Find Resultant Array After Removing Anagrams ( Similar to 142,49)
Description: Given array which contains Strings. If there is anagram so will be removed
Solutions:
Count Integers With Even Digit Sum
Description:
Given a positive integer num
, return the number of positive integers less than or equal to num
whose digit sums are even.
The digit sum of a positive integer is the sum of all its digits.
Solution:
Using brute-force
We run from 2 to n
If number is even ⇒ count ++
⇒ return count;
Counting Words With a Given Prefix
Description:
You are given an array of strings words
and a string pref
.
Return the number of strings in words
that contain pref
as a prefix.
A prefix of a string s
is any leading contiguous substring of s
.
Solution:
startsWith
() java built-in methodMost Frequent Number Following Key In an Array
e
e
e
Cells in a Range on an Excel Sheet
Largest Number After Digit Swaps by Parity
Add Two Integers (?? 😀 ??)
Divide Array Into Equal Pairs