diff --git a/ruby/1768.rb b/ruby/1768.rb deleted file mode 100644 index bd4c21f..0000000 --- a/ruby/1768.rb +++ /dev/null @@ -1,41 +0,0 @@ -require 'test/unit' - -#!NOT RECOMENDED -# Because of the slice! method changes the original string, loosing performance and -# create an new string in memory everytime it's called. -# def merge_alternately(word1, word2) -# result = "" - -# while word1.length > 0 || word2.length > 0 -# result += word1.slice!(0) if word1.length > 0 -# result += word2.slice!(0) if word2.length > 0 -# end - -# result -# end - -def merge_alternately(word1, word2) - result = "" - i = 0 - - while i < word1.length || i < word2.length - result << word1[i] if i < word1.length - result << word2[i] if i < word2.length - i += 1 - end - - result -end - -class TestMergeAlternately < Test::Unit::TestCase - def test_merge_alternately - assert_equal("apbqcr", merge_alternately("abc", "pqr")) - assert_equal("apbqrs", merge_alternately("ab", "pqrs")) - assert_equal("apbqcd", merge_alternately("abcd", "pq")) - assert_equal("", merge_alternately("", "")) - assert_equal("abc", merge_alternately("abc", "")) - assert_equal("apbqrs", merge_alternately("ab", "pqrs")) - # assert_equal("a1#*c2", merge_alternately("a#c", "1*2")) - # assert_equal("a c b", merge_alternately("a b", " c")) - end -end diff --git a/solutions/1701-1800/1768-merge-strings-alternately/README.md b/solutions/1701-1800/1768-merge-strings-alternately/README.md new file mode 100644 index 0000000..028fc3f --- /dev/null +++ b/solutions/1701-1800/1768-merge-strings-alternately/README.md @@ -0,0 +1,12 @@ +# 1768. Merge Strings Alternately + +[LeetCode problem 1768](https://leetcode.com/problems/merge-strings-alternately/) + +- **Difficulty**: Easy +- **Tags**: two-pointers, string + +## Description + +Given two strings `word1` and `word2`, merge them by adding letters in +alternating order, starting with `word1`. Once one string runs out, append +the remaining letters of the other. diff --git a/solutions/1701-1800/1768-merge-strings-alternately/solution.cpp b/solutions/1701-1800/1768-merge-strings-alternately/solution.cpp new file mode 100644 index 0000000..f05ec49 --- /dev/null +++ b/solutions/1701-1800/1768-merge-strings-alternately/solution.cpp @@ -0,0 +1,23 @@ +#include "solution.hpp" + +namespace leetcode::p1768 { + +std::string solve(const std::string& word1, const std::string& word2) { + std::string result; + result.reserve(word1.size() + word2.size()); + + std::size_t i = 0; + while (i < word1.size() || i < word2.size()) { + if (i < word1.size()) { + result += word1[i]; + } + if (i < word2.size()) { + result += word2[i]; + } + ++i; + } + + return result; +} + +} // namespace leetcode::p1768 diff --git a/solutions/1701-1800/1768-merge-strings-alternately/solution.hpp b/solutions/1701-1800/1768-merge-strings-alternately/solution.hpp new file mode 100644 index 0000000..a1d783f --- /dev/null +++ b/solutions/1701-1800/1768-merge-strings-alternately/solution.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +namespace leetcode::p1768 { + +// optimized: O(n + m) time, O(n + m) space — walk both strings once. +std::string solve(const std::string& word1, const std::string& word2); + +} // namespace leetcode::p1768 diff --git a/solutions/1701-1800/1768-merge-strings-alternately/test.cpp b/solutions/1701-1800/1768-merge-strings-alternately/test.cpp new file mode 100644 index 0000000..7e837f3 --- /dev/null +++ b/solutions/1701-1800/1768-merge-strings-alternately/test.cpp @@ -0,0 +1,11 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 1768", "[p1768]") { + CHECK(leetcode::p1768::solve("abc", "pqr") == "apbqcr"); + CHECK(leetcode::p1768::solve("ab", "pqrs") == "apbqrs"); + CHECK(leetcode::p1768::solve("abcd", "pq") == "apbqcd"); + CHECK(leetcode::p1768::solve("", "") == ""); + CHECK(leetcode::p1768::solve("abc", "") == "abc"); +}