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
相关推荐
SunnyDays10111 小时前
如何使用Python高效转换Excel到HTML
python·excel转html
priority_key1 小时前
排序算法:堆排序、快速排序、归并排序
java·后端·算法·排序算法·归并排序·堆排序·快速排序
Q_Q5110082851 小时前
python+django/flask的在线学习系统的设计与实现 积分兑换礼物
spring boot·python·django·flask·node.js·php
不染尘.2 小时前
2025_11_7_刷题
开发语言·c++·vscode·算法
Q_Q5110082852 小时前
python+django/flask的车辆尾气检测排放系统-可视化大屏展示
spring boot·python·django·flask·node.js·php
汤姆yu2 小时前
2026版基于python大数据的旅游可视化及推荐系统
python·旅游·大数据旅游
angleoldhen2 小时前
简单的智能数据分析程序
python·信息可视化·数据分析
来荔枝一大筐3 小时前
力扣 寻找两个正序数组的中位数
算法
算法与编程之美3 小时前
理解Java finalize函数
java·开发语言·jvm·算法
youzj09253 小时前
docker网站配置
python