From edcb573732be40e850e3a120ea1a1c7da8a37561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:19:35 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20Port=20problem=201768=20(Merge?= =?UTF-8?q?=20Strings=20Alternately)=20from=20Ruby=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces ruby/1768.rb with solutions/1701-1800/1768-merge-strings-alternately/, keeping the single O(n+m) approach it had (the file's commented-out slice!-based version was already marked "not recommended" and left out). --- ruby/1768.rb | 41 ------------------- .../1768-merge-strings-alternately/README.md | 12 ++++++ .../solution.cpp | 23 +++++++++++ .../solution.hpp | 10 +++++ .../1768-merge-strings-alternately/test.cpp | 11 +++++ 5 files changed, 56 insertions(+), 41 deletions(-) delete mode 100644 ruby/1768.rb create mode 100644 solutions/1701-1800/1768-merge-strings-alternately/README.md create mode 100644 solutions/1701-1800/1768-merge-strings-alternately/solution.cpp create mode 100644 solutions/1701-1800/1768-merge-strings-alternately/solution.hpp create mode 100644 solutions/1701-1800/1768-merge-strings-alternately/test.cpp 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"); +}