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
相关推荐
会编程的土豆6 分钟前
【数据结构与算法】 拓扑排序
数据结构·c++·算法
zth41302124 分钟前
SegmentSplay‘s Super STL(v2.2)
开发语言·c++·算法
数据知道31 分钟前
claw-code 源码详细分析:Bootstrap Graph——启动阶段图式化之后,排障与扩展为什么会变简单?
前端·算法·ai·bootstrap·claude code·claw code
Kel34 分钟前
从Prompt到Response:大模型推理端到端核心链路深度拆解
人工智能·算法·架构
Felven34 分钟前
D. Matryoshkas
算法
17(无规则自律)1 小时前
DFS连通域统计:岛屿数量问题及其变形
c++·算法·深度优先
ZC跨境爬虫1 小时前
极验滑动验证码自动化实战(ddddocr免费方案):本地缺口识别与Playwright滑动模拟
前端·爬虫·python·自动化
笨笨饿1 小时前
34_数据结构_栈
c语言·开发语言·数据结构·人工智能·嵌入式硬件·算法
单片机学习之路1 小时前
【Python】输入print函数
开发语言·前端·python
后藤十八里1 小时前
极验4消消乐验证码逆向笔记
笔记·爬虫·python