0% found this document useful (0 votes)
5 views

Java String Programs - Interview

The document outlines various string manipulation techniques commonly discussed in interviews, providing efficient solutions for each problem. Techniques include reversing strings, checking for palindromes, removing duplicates, and finding substrings, among others, with time and space complexity analyses. Each method is designed to optimize performance using different programming strategies.

Uploaded by

vishal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java String Programs - Interview

The document outlines various string manipulation techniques commonly discussed in interviews, providing efficient solutions for each problem. Techniques include reversing strings, checking for palindromes, removing duplicates, and finding substrings, among others, with time and space complexity analyses. Each method is designed to optimize performance using different programming strategies.

Uploaded by

vishal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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
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.
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.
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.
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.
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.
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.
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.
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)
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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy