diff options
-rw-r--r-- | add_space_string.c | 27 | ||||
-rw-r--r-- | add_space_string.cpp | 5 | ||||
-rw-r--r-- | add_space_string.py | 31 |
3 files changed, 48 insertions, 15 deletions
diff --git a/add_space_string.c b/add_space_string.c index 3d6bf04..6699ca8 100644 --- a/add_space_string.c +++ b/add_space_string.c @@ -1,7 +1,5 @@ // 2109. Adding Spaces to a String -#include <stdio.h> -#include <stdlib.h> -#include <string.h> +#include "leetcode.h" /* * given a 0-indexed string 's' and a 0-indexed integer array 'spaces' that @@ -10,16 +8,16 @@ * modified string after the spaces have been added */ -char *addSpaces(char *s, int *spaces, int spaces_size) { - int n = strlen(s), m = n + 1 + spaces_size; - char *ans = malloc(m * sizeof(char)); +char *addSpaces(char *s, int *spaces, int spacesSize) { + int n = strlen(s), m = n + spacesSize + 1; + char *ans = (char *)malloc(m * sizeof(char)); ans[m - 1] = '\0'; int aidx = 0, bidx = 0; - int *num = malloc(spaces_size * sizeof(int)); + int *num = (int *)malloc(spacesSize * sizeof(int)); num[0] = spaces[0]; - for (int i = 1; i < spaces_size; i++) + for (int i = 1; i < spacesSize; i++) num[i] = spaces[i] - spaces[i - 1]; - for (int i = 0; i < spaces_size; i++) { + for (int i = 0; i < spacesSize; i++) { strncpy(&ans[aidx], &s[bidx], num[i]); aidx += num[i]; bidx += num[i]; @@ -32,8 +30,11 @@ char *addSpaces(char *s, int *spaces, int spaces_size) { } int main() { - char s1[] = {"LeetcodeHelpsMeLearn"}, s2[] = {"icodeinpython"}; - int n1[] = {8, 13, 15}, n2[] = {1, 5, 7, 9}; - printf("%s\n", addSpaces(s1, n1, 3)); // expect: Leetcode Helps Me Learn - printf("%s\n", addSpaces(s2, n2, 4)); // expect: i code in py thon + char *s1 = "LeetcodeHelpsMeLearn", *s2 = "icodeinpython", *s3 = "spaceing"; + int n1[] = {8, 13, 15}, n2[] = {1, 5, 7, 9}, n3[] = {0, 1, 2, 3, 4, 5, 6}; + printf("%s\n", + addSpaces(s1, n1, ARRAY_SIZE(n1))); // expect: Leetcode Helps Me Learn + printf("%s\n", + addSpaces(s2, n2, ARRAY_SIZE(n2))); // expect: i code in py thon + printf("%s\n", addSpaces(s3, n3, ARRAY_SIZE(n3))); // s p a c i n g } diff --git a/add_space_string.cpp b/add_space_string.cpp index 2906d7a..9a9b7ea 100644 --- a/add_space_string.cpp +++ b/add_space_string.cpp @@ -27,9 +27,10 @@ public: int main() { Solution obj; - vector<int> s1 = {8, 13, 15}, s2 = {1, 5, 7, 9}; + vector<int> s1 = {8, 13, 15}, s2 = {1, 5, 7, 9}, s3 = {0, 1, 2, 3, 4, 5, 6}; cout << obj.addSpaces("LeetcodeHelpsMeLearn", s1) << endl; // expect: Leetcode Helps Me Learn cout << obj.addSpaces("icodeinpython", s2) - << endl; // expect: i code in py thon + << endl; // expect: i code in py thon + cout << obj.addSpaces("spacing", s3) << endl; // expect: s p a c i n g } diff --git a/add_space_string.py b/add_space_string.py new file mode 100644 index 0000000..eabebbf --- /dev/null +++ b/add_space_string.py @@ -0,0 +1,31 @@ +# 2109. Adding Spaces to a String + +""" +given a 0-indexed string 's' and a 0-indexed integer array 'spaces' that +describes the indices in the original string where spaces will be added. each +space should be inserted before the character at the given index. return the +modified string after the spaces have been added +""" + + +class Solution(object): + def addSpaces(self, s, spaces): + """ + :type s: str + :type spaces: List[int] + :rtype: str + """ + ans, j = [], 0 + for i, c in enumerate(s): + if j < len(spaces) and i == spaces[j]: + ans.append(" ") + j += 1 + ans.append(c) + return "".join(ans) + + +if __name__ == "__main__": + obj = Solution() + print(obj.addSpaces(s="LeetcodeHelpsMeLearn", spaces=[8, 13, 15])) + print(obj.addSpaces(s="icodeinpython", spaces=[1, 5, 7, 9])) + print(obj.addSpaces(s="spacing", spaces=[0, 1, 2, 3, 4, 5, 6])) |