알고리즘/LeetCode

[LeetCode/easy/String] Merge Strings Alternately

녕이 2022. 8. 16. 19:04
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