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
相关推荐
圣保罗的大教堂几秒前
leetcode 2943. 最大化网格图中正方形空洞的面积 中等
leetcode
阳光九叶草LXGZXJ17 分钟前
达梦数据库-学习-43-定时备份模式和删除备份(Python+Crontab)
linux·运维·开发语言·数据库·python·学习
独自破碎E19 分钟前
包含min函数的栈
android·java·开发语言·leetcode
wregjru20 分钟前
【C++】2.9异常处理
开发语言·c++·算法
CoovallyAIHub20 分钟前
如何用10%的标注数据,达到可媲美全监督模型的性能?AAAI 2026论文揭秘BCSI三大创新设计
深度学习·算法·计算机视觉
深蓝电商API22 分钟前
Scrapy与Splash结合爬取JavaScript渲染页面
javascript·爬虫·python·scrapy
AIFQuant23 分钟前
2026 澳大利亚证券交易所(ASX)API 接入与 Python 量化策略
开发语言·python·websocket·金融·restful
木头左23 分钟前
VIX期货基差异常下的指数期权波动率互换套利策略实现
python
肆悟先生26 分钟前
3.18 constexpr函数
开发语言·c++·算法
别在内卷了28 分钟前
三步搞定:双指针归并法求两个有序数组的中位数(Java 实现)
java·开发语言·学习·算法