diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/merge_string_alt.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/merge_string_alt.cpp')
-rw-r--r-- | other/merge_string_alt.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/other/merge_string_alt.cpp b/other/merge_string_alt.cpp new file mode 100644 index 0000000..d69c5fc --- /dev/null +++ b/other/merge_string_alt.cpp @@ -0,0 +1,32 @@ +// 1768. Merge Strings Alternately +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* + * given two strings 'word1' and 'word2', merge the strings by adding letters in + * alternating order, starting with 'word1'. if a string is longer than the + * other, append the additional letters onto the end of the merged string. + * return the merged string!! + */ + +char *mergeAlternately(char *word1, char *word2) { + char *ans = (char *)calloc(strlen(word1) + strlen(word2) + 1, sizeof(char)); + int i = 0, j = 0, k = 0; + while (i < strlen(word1) || j < strlen(word2)) { + if (i < strlen(word1)) + ans[k++] = word1[i++]; + if (j < strlen(word2)) + ans[k++] = word2[j++]; + } + return ans; +} + +int main() { + char w1[] = {"abc"}, w2[] = {"pqr"}, *ma1 = mergeAlternately(w1, w2); + char w3[] = {"ab"}, w4[] = {"pqrs"}, *ma2 = mergeAlternately(w3, w4); + char w5[] = {"abcd"}, w6[] = {"pq"}, *ma3 = mergeAlternately(w5, w6); + printf("%s\n", ma1); // expect: apbqcr + printf("%s\n", ma2); // expect: apbqrs + printf("%s\n", ma3); // expect: apbqcd +} |