728x90
https://leetcode.com/problems/merge-strings-alternately/
Merge Strings Alternately - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
string mergeAlternately(string word1, string word2) {
string answer = "";
int index = 0;
while(1){
if(word2.size() <= index || word1.size() <= index) break;
answer += word1[index];
answer += word2[index];
index++;
}
if(word1.size() > word2.size()) answer += word1.substr(index);
else if(word1.size() < word2.size()) answer += word2.substr(index);
return answer;
}
728x90
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode/easy/String] First Unique Character in a String (0) | 2022.09.20 |
---|---|
[LeetCode/easy/TwoPointer] Check If N and Its Double Exist (0) | 2022.08.17 |
[LeetCode/easy/String] Reverse Prefix of Word (0) | 2022.08.16 |
[LeetCode/easy/TwoPointer] Find First Palindromic String in the Array (0) | 2022.08.16 |
[LeetCode/easy/Array] Single Number (0) | 2022.08.16 |