One problem a day logo One problem a day
Browse problems
All problems 944 About this project

Categories

  • Array & Hashmap 217
  • Backtracking 33
  • Binary Search 27
  • Bit Manipulataion 25
  • Design 46
  • Dynamic Programming 85
  • Graph 74
  • Greedy 59
  • Heap 44
  • Intervals 16
  • Linked List 50
  • Math & Germetry 85
  • Queue 15
  • Sliding Window 29
  • Stack 48
  • Tree 122
  • Trie 14
  • Two Pointers 31

© 2026 One problem a day

Problems adding-spaces-to-a-string
array and hashmap December 30, 2022

adding-spaces-to-a-string

Time O(n) Space O(n) Open original problem

We will iterate over the string and add the spaces to the result.

class Solution:
    def addSpaces(self, s: str, spaces: List[int]) -> str:
        spaces = set(spaces)
        res = ''
        for i in range(len(s)):
            if i in spaces:
                res += ' ' + s[i]
            else:
                res += s[i]
        return res

Time complexity: O(n)
Space complexity: O(n)

Previous problem ← Remove letter to equalize frequency
Next problem 3sum smaller →

On this page

Start typing to search all 944 problems.
↑↓ Navigate ↵ Open Powered by Fuse.js