Java String Programs - Interview
Java String Programs - Interview
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
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.