Author: akshay

The goal of this problem is to determine whether a given string is a palindrome, considering only alphanumeric characters and ignoring cases. A palindrome reads the same forward and backward after removing non-alphanumeric characters. Steps Convert the string to lowercase to ensure case-insensitive comparison. Traverse each character in the string. Keep only alphanumeric characters using ASCII code checks. Build a filtered version and its reverse simultaneously. Return true if both strings are equal, else return false. Dry Run Input: s = “Race a Car” Lowercase: “race a car” Filtered alphanumerics: “raceacar” Reverse: “racacear” Since “raceacar” ≠ “racacear”, return false Time…

Read More

The problem requires reversing the first k characters for every 2k characters in a string. If there are fewer than k characters left, reverse all of them. If there are between k and 2k characters left, reverse the first k and leave the rest as is. Steps Convert the string into a character array (if needed). Iterate over the array in steps of 2k. At each step, reverse the next k characters if available. Join or return the modified string. Dry Run Input: s = “abcdefg”, k = 2 First 2k = 4 chars: reverse first 2 → “bacd” Remaining…

Read More

This problem focuses on splitting a string into the maximum number of balanced substrings. A string is considered balanced if it contains an equal number of ‘L’ and ‘R’ characters. The goal is to iterate through the string and count how many such balanced splits can be formed. Steps Initialize a counter variable to track balance. Traverse the string character by character. If the character is ‘R’, increment the counter. If the character is ‘L’, decrement the counter. Whenever the counter becomes zero, it means a balanced substring is found. Increment the result count. Return the total count. Dry Run…

Read More

This problem requires finding the most frequently occurring vowel and consonant characters in a string, and returning the sum of their frequencies. Steps Initialize a character frequency map using a loop. Define a list of vowels: [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]. Traverse the string and count how often each character appears. Track the highest frequency vowel and the highest frequency consonant. Return the sum of those two highest values. Dry Run Input: s = “successes” Frequency map: { s: 4, u: 1, c: 2, e: 2 } Vowels: u(1), e(2) → maxVowels = 2 Consonants: s(4), c(2) → maxConsonant =…

Read More

This problem is about finding the length of the last word in a given string. A word is defined as a maximal substring consisting of non-space characters only. Steps Start from the end of the string and skip any trailing spaces. Once a non-space character is found, start counting until a space is encountered again. The counted length is the length of the last word. Dry Run Input: “Hello World ” Skip trailing spaces → pointer now at index 10 (character ‘d’) Start counting: ‘d’, ‘l’, ‘r’, ‘o’, ‘W’ → total count = 5 Encounter space → stop Output: 5…

Read More

Problem Statement: You’re given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels. Letters are case sensitive, so “a” is different from “A”. Example: Input: jewels = “aA”, stones = “aAAbbbb”Output: 3 Input: jewels = “z”, stones = “ZZ”Output: 0 Constraints: 1 ≤ jewels.length, stones.length ≤ 50 jewels and stones consist of only English letters All characters in jewels are unique Approach 1 : Use two nested…

Read More

Problem Statement: You’re given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels. Letters are case sensitive, so “a” is different from “A”. Example: Input: jewels = “aA”, stones = “aAAbbbb”Output: 3 Input: jewels = “z”, stones = “ZZ”Output: 0 Constraints: 1 ≤ jewels.length, stones.length ≤ 50 jewels and stones consist of only English letters All characters in jewels are unique Approach: Use a Set (or hash…

Read More

Problem Statement: You are given a 0-indexed array of strings words and a character x. Return an array of indices representing the words that contain the character x. The returned array can be in any order. Example: Input: words = [“leet”, “code”], x = “e”Output: [0, 1] Input: words = [“abc”, “bcd”, “aaaa”, “cbc”], x = “a”Output: [0, 2] Input: words = [“abc”, “bcd”, “aaaa”, “cbc”], x = “z”Output: [] Constraints: 1 ≤ words.length ≤ 50 1 ≤ words[i].length ≤ 50 x is a lowercase English letter words[i] consists only of lowercase English letters Approach: Use two nested loops: Outer…

Read More

Problem Statement: Given a string s consisting of words and spaces, return the length of the last word in the string. A word is defined as a maximal substring consisting of non-space characters only. Example: Input: s = “Hello World”Output: 5 Input: s = ” fly me to the moon “Output: 4 Input: s = “luffy is still joyboy”Output: 6 Constraints: 1 ≤ s.length ≤ 104 s consists of only English letters and spaces ‘ ‘ There will be at least one word in s Approach: Start from the end and skip trailing spaces. Count characters until the next space…

Read More

Problem Statement: Given a string s consisting of words and spaces, return the length of the last word in the string. A word is defined as a maximal substring consisting of non-space characters only. Example: Input: s = “Hello World”Output: 5The last word is “World” with length 5. Input: s = ” fly me to the moon “Output: 4The last word is “moon” with length 4. Input: s = “luffy is still joyboy”Output: 6The last word is “joyboy” with length 6. Constraints: 1 ≤ s.length ≤ 104 s consists of only English letters and spaces ‘ ‘ There will be…

Read More