Made with Xodo PDF Reader and Editor
STRING MANIPULATION - Interview Point Of view
Contents
1. Reverse a String ........................................................................................................ 2
2. Check if a String is a Palindrome............................................................................. 2
3. Remove Duplicates from a String ............................................................................ 2
4. Find the First Non-Repeating Character ................................................................. 3
5. Count the Occurrences of Each Character ............................................................. 3
6. Reverse Words in a Sentence .................................................................................. 3
7. Check if Two Strings are Anagrams ........................................................................ 4
8. Find the Longest Substring Without Repeating Characters .................................. 4
9. Convert a String to an Integer (atoi Implementation) ............................................. 5
10. Compress a String (Run-Length Encoding) .......................................................... 5
11. Find the Most Frequent Character ......................................................................... 6
12. Find All Substrings of a Given String .................................................................... 6
13. Check if a String is a Rotation of Another String ................................................. 7
14. Remove All White Spaces from a String ............................................................... 7
15. Check if a String is a Valid Shuffle of Two Strings ............................................... 7
16. Convert a String to Title Case ................................................................................ 8
17. Find the Longest Common Prefix .......................................................................... 8
18. Convert a String to a Character Array ................................................................... 8
19. Replace Spaces with %20 (URL Encoding) ........................................................... 9
20. Convert a Sentence into an Acronym .................................................................... 9
21. Check if a String Contains Only Digits .................................................................. 9
22. Find the Number of Words in a String ................................................................... 9
23. Remove a Given Character from a String ............................................................. 9
24. Find the Shortest Word in a String ...................................................................... 10
25. Find the Longest Palindromic Substring ............................................................ 10
Made with Xodo PDF Reader and Editor
1. Reverse a String
Efficient Solution: Use StringBuilder (O(n) time, O(n) space)
Why? StringBuilder’s reverse() method is optimized and avoids extra loops.
2. Check if a String is a Palindrome
Efficient Solution: Two-pointer approach (O(n) time, O(1) space)
Why? Avoids extra space used by recursion or string builders.
3. Remove Duplicates from a String
Efficient Solution: Use a HashSet (O(n) time, O(n) space)
Why? Uses a HashSet for O(1) lookups.
Made with Xodo PDF Reader and Editor
4. Find the First Non-Repeating Character
Efficient Solution: Use a frequency map (O(n) time, O(1) space)
Why? Only two passes over the string, avoiding nested loops
5. Count the Occurrences of Each Character
Efficient Solution: Use an array (O(n) time, O(1) space)
Why? Uses a single pass and avoids sorting.
6. Reverse Words in a Sentence
Efficient Solution: Use split() and StringBuilder (O(n) time, O(n) space)
Why? Splitting and reversing words directly is faster than handling each character.
Made with Xodo PDF Reader and Editor
7. Check if Two Strings are Anagrams
Efficient Solution: Use frequency counting (O(n) time, O(1) space)
Why? Avoids sorting (O(n log n)), making it O(n) time complexity.
8. Find the Longest Substring Without Repeating Characters
Efficient Solution: Sliding window technique (O(n) time, O(min(n, alphabet)) space)
Why? Uses a single pass and a HashMap to track character positions efficiently.
Made with Xodo PDF Reader and Editor
9. Convert a String to an Integer (atoi Implementation)
Efficient Solution: Manual parsing (O(n) time, O(1) space)
Why? Efficient handling of overflow and signs.
10. Compress a String (Run-Length Encoding)
Efficient Solution: Two-pointer approach (O(n) time, O(n) space)
Why? Uses O(n) time with a single pass.
Made with Xodo PDF Reader and Editor
11. Find the Most Frequent Character
Efficient Solution: Use a frequency array (O(n) time, O(1) space)
Why? Avoids sorting and maps, making it O(n).
12. Find All Substrings of a Given String
Efficient Solution: Generate substrings using two loops (O(n²) time, O(1) space)
Why? Uses substring efficiently, avoiding redundant operations.
Made with Xodo PDF Reader and Editor
13. Check if a String is a Rotation of Another String
Efficient Solution: Use concatenation (O(n) time, O(n) space)
Why? Eliminates the need for nested loops.
14. Remove All White Spaces from a String
Efficient Solution: Use replaceAll() (O(n) time, O(1) space)
Why? Uses regex to remove all spaces in a single pass.
15. Check if a String is a Valid Shuffle of Two Strings
Efficient Solution: Sorting + Two-Pointer (O(n log n) time, O(n) space)
Why? Sorting makes comparison simpler.
Made with Xodo PDF Reader and Editor
16. Convert a String to Title Case
Efficient Solution: Use split() and StringBuilder (O(n) time, O(n) space)
Why? Processes words in a single pass.
17. Find the Longest Common Prefix
Efficient Solution: Sort and compare first & last strings (O(n log n) time, O(1) space)
Why? Sorting helps quickly find the common prefix.
18. Convert a String to a Character Array
Efficient Solution: Use toCharArray() (O(n) time, O(n) space)
Why? Most efficient way in Java.
Made with Xodo PDF Reader and Editor
19. Replace Spaces with %20 (URL Encoding)
Efficient Solution: Use replace() (O(n) time, O(1) space)
Why? Avoids manual iteration.
20. Convert a Sentence into an Acronym
Efficient Solution: Use split() and StringBuilder (O(n) time, O(n) space)
Why? Processes words efficiently.
21. Check if a String Contains Only Digits
Efficient Solution: Use regex (O(n) time, O(1) space)
Why? Single regex check avoids looping.
22. Find the Number of Words in a String
Efficient Solution: Use split() (O(n) time, O(n) space)
Why? Uses regex to efficiently count words.
23. Remove a Given Character from a String
Efficient Solution: Use replace() (O(n) time, O(1) space)
Made with Xodo PDF Reader and Editor
Why? More optimized than manually iterating.
24. Find the Shortest Word in a String
Efficient Solution: Use split() and min() (O(n) time, O(n) space)
Why? Uses Java Streams for concise logic.
25. Find the Longest Palindromic Substring
Efficient Solution: Expand Around Center (O(n²) time, O(1) space)
Why? Faster than brute force.