1️⃣ Check if a String is a Palindrome
Problem
Given a string, check if it reads the same forward and backward. Example: "madam" → palindrome, "hello" → not palindrome.
Idea
Use two pointers: one at the beginning, one at the end, and compare characters.
2️⃣ Reverse a String (Without Using Library Reverse)
Problem
Reverse a given string: input "hello" → output "olleh".
Idea
Convert to char array and swap from both ends.
3️⃣ Reverse Words in a Sentence
Problem
Given "Java is awesome", output "awesome is Java" (reverse word order, not characters inside each word).
Idea
Split on spaces, reverse the array of words, then join back.
4️⃣ Check if Two Strings are Anagrams
Problem
Two strings are anagrams if they contain the same characters with the same frequency (order doesn’t matter).
Example: "listen" and "silent" → anagrams.
Idea
Either sort both strings and compare, or count character frequencies. Here we sort.
5️⃣ Find the First Non-Repeating Character
Problem
Given a string, find the first character that does not repeat. Example: "swiss" → first non-repeating is 'w'.
Idea
First pass: count frequencies. Second pass: return first char with count 1.
6️⃣ Count Vowels and Consonants
Problem
Given a string, count how many vowels and consonants are present (alphabet letters only).
Idea
Loop through characters, check if alphabet, then check if it’s in 'aeiou'.
7️⃣ Remove All Duplicates Characters (Keep First Occurrence)
Problem
Input: "programming" → Output: "progamin" (remove repeated chars, keep the first time they appear).
Idea
Use a boolean array or Set to track already seen characters.
8️⃣ Check if One String is a Rotation of Another
Problem
Check if s2 is a rotation of s1.
Example: s1 = "waterbottle", s2 = "erbottlewat" → rotation.
Idea
If lengths are same, check if s2 is a substring of s1 + s1.
9️⃣ Longest Substring Without Repeating Characters
Problem
Given a string, find the length of the longest substring without repeating characters.
Example: "abcabcbb" → longest is "abc" → length 3.
Idea
Use sliding window with a map that stores last index of each character.
🔟 Character Frequency Count
Problem
Print how many times each character appears in a string.
Example: "banana" → b:1, a:3, n:2.
Idea
Use a Map<Character, Integer> and count all characters.
1️⃣1️⃣ Count Occurrences of a Substring
Problem
Given a string text and a string pattern, count how many times pattern appears in text (non-overlapping).
Example: text = "aaaa", pattern = "aa" → result = 2.
Idea
Scan using indexOf in a loop and move index by pattern length.
1️⃣2️⃣ Simple String Compression (Run-Length Encoding Style)
Problem
Compress a string by replacing consecutive repeating characters with the character followed by the count.
Example: "aaabbc" → "a3b2c1".
If compressed string is not smaller, return original.
Idea
Loop through string and count runs.
No comments:
Post a Comment