Leetcode 1768. Merge Strings Alternately

Problem

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...

Algorithm

Insert numbers alternately

Code

python3 复制代码
class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        ans = ""
        L1, L2, P1, P2 = len(word1), len(word2), 0, 0
        while P1 < L1 or P2 < L2:
            if P1 < L1:
                ans += word1[P1]
                P1 += 1
            if P2 < L2:
                ans += word2[P2]
                P2 += 1
        return ans
相关推荐
弱冠少年15 分钟前
websockets库使用(基于Python)
开发语言·python·numpy
自由的dream28 分钟前
0-1背包问题
算法
2401_8572979134 分钟前
招联金融2025校招内推
java·前端·算法·金融·求职招聘
技术无疆38 分钟前
【Python】Streamlit:为数据科学与机器学习打造的简易应用框架
开发语言·人工智能·python·深度学习·神经网络·机器学习·数据挖掘
羊小猪~~1 小时前
机器学习/数据分析--用通俗语言讲解时间序列自回归(AR)模型,并用其预测天气,拟合度98%+
人工智能·python·机器学习·数据挖掘·数据分析·回归·时序数据库
爱上语文1 小时前
Java LeetCode每日一题
java·开发语言·leetcode
qq_273900231 小时前
解析TMalign文本文件中的转换矩阵
python·生物信息学
阿华的代码王国2 小时前
【JavaEE】——文件IO的应用
开发语言·python
良月澪二2 小时前
CSP-S 2021 T1廊桥分配
算法·图论
电饭叔2 小时前
《python语言程序设计》2018版第8章19题几何Rectangle2D类(下)-头疼的几何和数学
开发语言·python