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
相关推荐
helloweilei2 小时前
python 抽象基类
python
用户8356290780512 小时前
Python 实现 PPT 转 HTML
后端·python
CoovallyAIHub4 小时前
语音AI Agent编排框架!Pipecat斩获10K+ Star,60+集成开箱即用,亚秒级对话延迟接近真人反应速度!
深度学习·算法·计算机视觉
木心月转码ing6 小时前
Hot100-Day14-T33搜索旋转排序数组
算法
zone77398 小时前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone77398 小时前
005:RAG 入门-LangChain读取表格数据
后端·python·agent
会员源码网8 小时前
内存泄漏(如未关闭流、缓存无限增长)
算法
颜酱10 小时前
从0到1实现LFU缓存:思路拆解+代码落地
javascript·后端·算法
颜酱11 小时前
从0到1实现LRU缓存:思路拆解+代码落地
javascript·后端·算法
树獭非懒1 天前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm