Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions ruby/1768.rb

This file was deleted.

12 changes: 12 additions & 0 deletions solutions/1701-1800/1768-merge-strings-alternately/README.md
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 23 additions & 0 deletions solutions/1701-1800/1768-merge-strings-alternately/solution.cpp
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions solutions/1701-1800/1768-merge-strings-alternately/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <string>

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
11 changes: 11 additions & 0 deletions solutions/1701-1800/1768-merge-strings-alternately/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <catch2/catch_test_macros.hpp>

#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");
}
Loading