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
相关推荐
春日见4 分钟前
如何入门端到端自动驾驶?
linux·人工智能·算法·机器学习·自动驾驶
FreakStudio10 分钟前
保姆级 uPyPi 教程|从 0 到 1:MicroPython 驱动包一键安装 + 分享全攻略
python·嵌入式·电子diy
清水白石00812 分钟前
Python 对象序列化深度解析:pickle、JSON 与自定义协议的取舍之道
开发语言·python·json
2401_8769075223 分钟前
Python机器学习实践指南
开发语言·python·机器学习
图图的点云库35 分钟前
高斯滤波实现算法
c++·算法·最小二乘法
张张123y1 小时前
RAG从0到1学习:技术架构、项目实践与面试指南
人工智能·python·学习·面试·架构·langchain·transformer
Shi_haoliu1 小时前
openClaw源码部署-linux
前端·python·ai·openclaw
gf13211111 小时前
python_查询并删除飞书多维表格中的记录
java·python·飞书
一叶落4381 小时前
题目:15. 三数之和
c语言·数据结构·算法·leetcode