Adding Spaces to a String - Problem
Transform a string by inserting spaces at specific positions!

You are given a string s and an array of indices spaces. Your task is to insert spaces before the characters at the specified indices to create a new formatted string.

๐ŸŽฏ Goal: Return the modified string with spaces added at the correct positions
๐Ÿ“ฅ Input: A string s and an integer array spaces containing indices
๐Ÿ“ค Output: The string with spaces inserted before characters at given indices

Example: Given s = "EnjoyYourCoffee" and spaces = [5, 9], we insert spaces before index 5 ('Y') and index 9 ('C') to get "Enjoy Your Coffee".

Input & Output

example_1.py โ€” Basic Word Separation
$ Input: s = "EnjoyYourCoffee"<br>spaces = [5, 9]
โ€บ Output: "Enjoy Your Coffee"
๐Ÿ’ก Note: We insert spaces before indices 5 ('Y') and 9 ('C'), creating natural word boundaries to form readable text.
example_2.py โ€” Multiple Small Words
$ Input: s = "icodeinpython"<br>spaces = [1, 5, 7, 9]
โ€บ Output: "i code in python"
๐Ÿ’ก Note: Spaces are inserted before indices 1, 5, 7, and 9, transforming a concatenated string into separate words.
example_3.py โ€” Single Space at Beginning
$ Input: s = "spacing"<br>spaces = [0]
โ€บ Output: " spacing"
๐Ÿ’ก Note: A space is inserted at the very beginning (index 0), demonstrating edge case handling for the first position.

Visualization

Tap to expand
Adding Spaces to Transform TextInput String:E0n1j2o3y4Y5o6u7r8C9o10f11Spaces Array: [5, 9]59Two Pointers AlgorithmOutput String:EnjoyโŽตYourโŽตCofAlgorithm Steps:1. Use two pointers: i for string position, j for spaces array position2. When i equals spaces[j], insert space and increment j3. Always add current character and increment iOrange boxes show positions where spaces are inserted
Understanding the Visualization
1
Scan Input
Examine the string and note positions marked for space insertion
2
Build Result
Process each character, adding spaces when reaching marked positions
3
Two Pointers
Use one pointer for string position and another for spaces array
4
Final String
Combine all characters and inserted spaces into final result
Key Takeaway
๐ŸŽฏ Key Insight: Using two pointers allows us to process the string and space positions simultaneously in optimal O(n + m) time, taking advantage of the sorted spaces array to avoid expensive lookups.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

Single pass through string of length n and spaces array of length m

n
2n
โœ“ Linear Growth
Space Complexity
O(n + m)

Space for result string which will have length n + m

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 3 ร— 104
  • s consists only of lowercase and uppercase English letters
  • 1 โ‰ค spaces.length โ‰ค 3 ร— 104
  • 0 โ‰ค spaces[i] โ‰ค s.length - 1
  • All the values of spaces are strictly increasing
Asked in
Amazon 25 Microsoft 18 Google 15 Meta 12
28.5K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen