From 635a34eb9128877ae1bbdb72c101f2fc59713b9a Mon Sep 17 00:00:00 2001 From: bumpsoo Date: Tue, 3 Dec 2024 21:25:30 +0900 Subject: [PATCH] https://leetcode.com/problems/adding-spaces-to-a-string --- medium/adding_spaces_to_a_string.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 medium/adding_spaces_to_a_string.py diff --git a/medium/adding_spaces_to_a_string.py b/medium/adding_spaces_to_a_string.py new file mode 100644 index 0000000..43970d6 --- /dev/null +++ b/medium/adding_spaces_to_a_string.py @@ -0,0 +1,14 @@ +# https://leetcode.com/problems/adding-spaces-to-a-string +from typing import List + +class Solution: + def addSpaces(self, s: str, spaces: List[int]) -> str: + result = [] + last_index = 0 + for index in spaces: + result.append(s[last_index:index]) + result.append(" ") + last_index = index + result.append(s[last_index:len(s)]) + return "".join(result) +