1. Minimum Moves to Convert String

Description:

You are given a string s consisting of n characters which are either 'X' or 'O'.

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:

  1. Every time we hit ‘X’ we will move to next 3

  2. Find Target Indices After Sorting Array

Description:

You are given a 0-indexed integer array nums and a target element target.

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:

  1. 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

  2. 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.

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:

  1. 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

  2. A Number After a Double Reversal

    Description:

    Reversing an integer means to reverse all its digits.

    Given an integer numreverse num to get reversed1then reverse reversed1 to get reversed2. Return true if reversed2 equals num. Otherwise return false.

    Solution:

    1. Using normal approach

      Because when

  3. 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:

  1. Count Elements With Strictly Smaller and Greater Elements

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:

  1. Find min and max

  2. Compare if the number > min and number < max ( ⇒ so every number will have at least 2 strict number ) ⇒ count++;

  3. ⇒ Return count; ( Using normal way faster than using stream)

  4. 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 numLeading zeros are allowed in new1 and new2, and all the digits found in num must be used.

    Return the minimum possible sum of new1 and new2.

    Solution:

    1. Using greedy algorithm

      We always pick the next smallest to build the smallest sum

      [new1,new2] ⇒ always pick the pick smallest one for new1

  5. 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.

    Return the number of operations required to make either num1 = 0 or num2 = 0.

    Solution:

    1. Using normal approach

    2. Using Euclidean(best) - like GCD simulation

      image.png

  6. 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:

    1. Check array[i] and array[i-1] where i start from 1 to length-1. If they are anagram ⇒ skip, otherwise, ⇒ add to list of result.
  7. 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:

  1. Using brute-force

    We run from 2 to n

    If number is even ⇒ count ++

    ⇒ return count;

  2. 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.

    prefix of a string s is any leading contiguous substring of s.

    Solution:

    1. Using startsWith() java built-in method
  3. Most Frequent Number Following Key In an Array

  4. e

  5. e

  6. e

  7. Cells in a Range on an Excel Sheet

  8. Largest Number After Digit Swaps by Parity

  9. Add Two Integers (?? 😀 ??)

  10. Divide Array Into Equal Pairs