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
相关推荐
THMAIL3 分钟前
机器学习从入门到精通 - Transformer颠覆者:BERT与预训练模型实战解析
python·随机森林·机器学习·分类·bootstrap·bert·transformer
0wioiw015 分钟前
Python基础(①⑧Queue)
windows·python
Source.Liu36 分钟前
【Python自动化】 21 Pandas Excel 操作完整指南
python·excel·pandas
urhero40 分钟前
Python 制作的一个小说在线阅读工具
python·免费·小说·应用软件·小说在线阅读·无广告
跟橙姐学代码1 小时前
列表、元组与字典:Python开发者的三大必备利器,再向高手靠近一步
前端·python·ipython
计算机毕设残哥1 小时前
HDFS存储农业大数据的秘密是什么?高级大豆数据分析与可视化系统架构设计思路
大数据·hadoop·python·hdfs·数据分析·spark·django
蛋仔聊测试1 小时前
pytest源码解析(二)剖析 pytest 的核心组件
python·面试
DDAshley1261 小时前
【PaddleOCR】从零开始训练自己的模型--详细教程
算法·计算机视觉
梁辰兴1 小时前
数据结构:查找
数据结构·算法·查找·顺序查找·折半查找·分块查找