Merge Strings Alternately - LeetCode

https://leetcode.com/problems/merge-strings-alternately/?envType=study-plan-v2&envId=leetcode-75

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 letters onto the end of the merged string.

Return the merged string.

Example 1:

Input: word1 = "abc", word2 = "pqr"
Output: "apbqcr"
Explanation: The merged string will be merged as so:
word1:  a   b   c
word2:    p   q   r
merged: a p b q c r

Example 2:

Input: word1 = "ab", word2 = "pqrs"
Output: "apbqrs"
Explanation: Notice that as word2 is longer, "rs" is appended to the end.
word1:  a   b
word2:    p   q   r   s
merged: a p b q   r   s

Example 3:

Input: word1 = "abcd", word2 = "pq"
Output: "apbqcd"
Explanation: Notice that as word1 is longer, "cd" is appended to the end.
word1:  a   b   c   d
word2:    p   q
merged: a p b q c   d

Constraints:

  • 1 <= word1.length, word2.length <= 100
  • word1 and word2 consist of lowercase English letters.

解析:

这75道比pandas难不少。这道题的意思是将两个字符串交替排序,互相穿插,始终以word1开始,最终剩下的跟在最后。

Py

class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        result = ''
        for i in range(min(len(word1), len(word2))):
            result += word1[i]
            result += word2[i]
        if len(word1)>len(word2):
            result += word1[i+1:]

        if len(word1)<len(word2):
            result += word2[i+1:]
        
        return result
class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        merged = ''.join(a + b for a, b in zip(word1, word2))
        return merged + word1[len(word2):] + word2[len(word1):]

Java

class Solution {
    public String mergeAlternately(String word1, String word2) {
        StringBuilder result = new StringBuilder();
        int i;
        for(i = 0 ; i<Math.min(word1.length(),word2.length());i++){
            result.append(word1.charAt(i));
            result.append(word2.charAt(i));
        }
        if (word1.length()>word2.length()){
            result.append(word1.substring(i));
        }
        if (word1.length()<word2.length()){
            result.append(word2.substring(i));
        }

        return result.toString();
        
    }
}

可以用三元运算符改进后面的判断。

class Solution {
    public String mergeAlternately(String word1, String word2) {
        StringBuilder result = new StringBuilder();
        int i;
        for(i = 0; i < Math.min(word1.length(), word2.length()); i++) {
            result.append(word1.charAt(i));
            result.append(word2.charAt(i));
        }

        if (word1.length() != word2.length()) {
            result.append((word1.length() > word2.length()) ? word1.substring(i) : word2.substring(i));
        }

        return result.toString();
    }
}

C++

class Solution {
public:
    string mergeAlternately(string word1, string word2) {
        string result;
        int i;
        for (i = 0; i < min(word1.size(),word2.size()); ++i) {
            result += word1[i];
            result += word2[i];
        }

        if (word1.size() != word2.size()){
            return result += (word1.size() > word2.size()) ? word1.substr(i): word2.substr(i);
        }
        return result;
    }
};