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
相关推荐
敲代码中11 分钟前
位运算[找出唯一成对的数]
java·算法·排序算法
换个网名有点难26 分钟前
django定时任务方案比对
python·django
trust Tomorrow26 分钟前
每日一题-力扣-2712: 使所有字符相等的最小成本 0327
python·算法·leetcode
蒲小英1 小时前
算法-回溯算法
数据结构·算法
·醉挽清风·1 小时前
学习笔记—数据结构—二叉树顺序实现(小堆)
c语言·数据结构·c++·笔记·学习·算法
王有品1 小时前
python之size,count的区别
python·机器学习·pandas
老马啸西风1 小时前
MOSN(Modular Open Smart Network)是一款主要使用 Go 语言开发的云原生网络代理平台
网络·后端·算法·阿里云·云原生·中间件·golang
Rsecret22 小时前
个人学习编程(3-24) 数据结构
学习·算法
医学生_R语言2 小时前
【R语言可视化】相关系数热图
开发语言·python·r语言
蹦蹦跳跳真可爱5892 小时前
Python----计算机视觉处理(Opencv:凸包特征检测:凸包方法)
人工智能·python·opencv·计算机视觉