Introduction
The Remove Spaces from String problem involves removing all space characters from a string.
Given a string s, the task is to:
- remove all blank spaces
- return the modified string
This problem helps in understanding:
- string traversal
- filtering techniques
- character processing
- text cleaning operations
Example
Input:s = "hello world"
Output:
"helloworld"
Explanation:
All spaces are removed from the string.
Input:
s = " c o d i n g "
Output:
"coding"
Explanation:
Only non-space characters are kept.
Constraints
1 <= s.length <= 10^5s contains printable ASCII charactersApproach 1 : Brute Force (Built-in Method)
Explanation
The simplest way to solve this problem is:
- Use built-in replace function
- Replace spaces with empty string
This approach is easy and efficient.
Steps
- Take input string.
- Replace spaces using built-in method.
- Return modified string.
Dry Run
Input:s = "hello world"
Replace spaces:
"hello world"
→
"helloworld"
Final Result:
"helloworld"
Brute Force Code
Complexity Analysis
Time Complexity: O(n)Explanation:
Each character is processed once.
Space Complexity: O(n)
Explanation:
Modified string is created.
Approach 2 : Optimized Solution (Manual Traversal)
Explanation
Instead of using built-in methods, we can manually traverse the string.
The idea is:
- Traverse every character
- Ignore spaces
- Store only non-space characters
This helps understand filtering logic clearly.
Steps
- Create empty result string.
- Traverse all characters.
- If character is not space:
- append into result
- Return final string.
Dry Run
Input:s = "hello world"
Traverse h:
Not space
Result:
"h"
Traverse e:
Not space
Result:
"he"
Continue traversal...
Traverse space:
Ignore character
Continue traversal...
Final Result:
"helloworld"
Optimized Code
Complexity Analysis
Time Complexity: O(n)Explanation: Each character is traversed once.
Space Complexity: O(n)
Explanation: Extra result string is used.
- String contains no spaces
- String contains only spaces
- Multiple consecutive spaces
- String contains symbols
- String contains numbers
Why This Problem is Important
This problem helps in understanding:
- String traversal
- Character filtering
- Text cleaning
- Conditional processing
- Manual string manipulation
It is one of the most common beginner-level string interview problems.
Real-World Applications
Space removal concepts are used in:
- Text preprocessing
- Search engines
- Data cleaning systems
- NLP pipelines
- Input sanitization
Common Mistakes
- Forgetting multiple spaces
- Incorrect condition checks
- String concatenation errors
- Modifying non-space characters
Interview Tips
Interviewers often expect:
- Proper traversal logic
- Efficient filtering approach
- String manipulation understanding
Always explain why spaces are skipped during traversal.
Related Questions
- Reverse String
- Lowercase Conversion
- Valid Palindrome
- Remove Duplicates
- String Compression
Final Takeaway
The Remove Spaces from String problem is a fundamental string manipulation problem that teaches filtering and traversal techniques. Understanding this problem builds a strong foundation for advanced text processing and string-based interview problems.