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
相关推荐
invincible_Tang几秒前
AcWing 796. 子矩阵的和 _
数据结构·算法
喵手几秒前
Python爬虫高阶:用 Playwright “监听” Figma 社区热门插件数据!
爬虫·python·爬虫实战·figma·playwright·零基础python爬虫教学·社区热门插件数据采集
MoRanzhi12031 分钟前
Pillow 图像滤波、卷积与边缘处理
图像处理·python·计算机视觉·pillow·卷积·边缘检测·图像滤波
怪侠_岭南一只猿2 分钟前
爬虫学习阶段三:动态网页爬取(完整学习文档)
爬虫·python·学习
南 阳4 分钟前
Python从入门到精通day48
开发语言·python
虎大猫猫5 分钟前
JupyterLab的安装与使用完全指南
ide·python·jupyter
米粒15 分钟前
力扣算法刷题 Day 8
算法·leetcode·职场和发展
web3.08889999 分钟前
如何确保1688商品数据API接口的安全性
python
<-->11 分钟前
SGLang 相比 vLLM 的主要优势
人工智能·pytorch·python·transformer