반응형
https://leetcode.com/problems/merge-strings-alternately/description/
Merge Strings Alternately - LeetCode
Can you solve this real interview question? Merge Strings Alternately - You are 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 le
leetcode.com
선언한 문자열에 word1과 word2를 번갈아가면서 삽입해줍니다. 한 문자열이 끝에 다다르면, 남은 문자열을 모두 삽입해줍니다.
class Solution {
public:
string mergeAlternately(string word1, string word2) {
string res = "";
int i = 0, sz = min(word1.size(), word2.size());
while(i < sz) {
res.push_back(word1[i]);
res.push_back(word2[i++]);
}
return res + word1.substr(i) + word2.substr(i);
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 662. Maximum Width of Binary Tree (0) | 2023.04.20 |
---|---|
LeetCode 1372. Longest ZigZag Path in a Binary Tree (0) | 2023.04.19 |
LeetCode 1431. Kids With the Greatest Number of Candies (0) | 2023.04.18 |
LeetCode 1639. Number of Ways to Form a Target String Given a Dictionary (0) | 2023.04.16 |
LeetCode 516. Longest Palindromic Subsequence (0) | 2023.04.14 |