Shadab Ali

Jul 03, 2025 • 2 min read

Arrays, Strings, Stack & Binary Search

Build Confidence super easy Question on LeetCode

Arrays, Strings, Stack & Binary Search

Perfect — you've already documented your progress really well on core Linked List problems! Here's a continuation post to cover the remaining problems you solved today. You can title this something like:


🔥

Today’s focus was not just solving problems, but refining problem-solving intuition across strings, stacks, and binary search—here’s what I tackled:


1. Length of Last Word

Problem: Return the length of the last word in a given string.
My Approach:

  • Traversed from the end to skip trailing spaces.

  • Counted until the next space (or beginning) appeared.
    No need to split or use STL functions like trim.


2. Find Words Containing a Character

Problem: Return indices of words that contain a given character.
My Approach:

  • Nested loop to search character in each word.

  • Can be optimized using std::string::find instead of full iteration.
    Focus: Efficient scanning within strings.


3. Max Frequency of Vowels and Consonants

Problem: Find the most frequent vowel and consonant and return their sum.
My Approach:

  • Counted letter frequency using a 26-size array.

  • Checked for vowels by comparing indexes (a, e, i, o, u).
    Classic frequency array use case.


4. Jewels and Stones

Problem: Count how many stones are also jewels.
My Approach:

  • Iterated stones, used jewels.find() for each char.

  • Optimized version could use unordered_set for constant lookup.
    Practiced brute-force first, then optimized.


5. Valid Parentheses

Problem: Validate if the bracket string is properly balanced.
My Approach:

  • Used a stack to store opening brackets.

  • Matched and popped on valid closing brackets.
    Important for practicing stack fundamentals.


6. Implement Stack Using Queue

Problem: Simulate stack operations using queues only.
My Approach (1-Queue):

  • Pushed elements and rotated the queue to maintain order.
    Smart technique to convert FIFO to LIFO.


7. Guess Number Higher or Lower

Problem: Guess the picked number using limited API.
My Approach:

  • Classic binary search to minimize queries.
    Clean log(n) solution using s + (e - s) / 2 to avoid overflow.


8. Largest Odd Number in String

Problem: Return the largest-valued odd substring.
My Approach:

  • Scanned from right to find first odd digit.

  • Returned substring up to that index.
    Handled char-to-int conversion carefully.


9. Longest Common Prefix

Problem: Return the common prefix string from an array of strings.
My Approach:

  • Used the first string as a base and shrunk it while comparing.
    Saves time instead of character-by-character brute-force.


🔚 What I Learned Today:

✅ Writing clean string traversal logic
✅ Mastering stack-based problems
✅ Building real confidence in binary search
✅ Optimizing code by identifying repeated patterns
✅ Better understanding of input constraints for performance


📅 Continuing the Grind
Up next:

Binary Search Deepe Down

Join Shadab on Peerlist!

Join amazing folks like Shadab and thousands of other builders on Peerlist.

peerlist.io/

It’s available... this username is available! 😃

Claim your username before it's too late!

This username is already taken, you’re a little late.😐

0

2

0