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
相关推荐
fu的博客12 分钟前
【数据结构7】链式栈实现
数据结构·算法
xiaoye-duck13 分钟前
《算法题讲解指南:优选算法-双指针》--01移动零,02复写零
c++·算法
田里的水稻17 分钟前
LPC_激光点云定位(LSLAM)-(IPC)
人工智能·算法·数学建模·机器人·自动驾驶
一切尽在,你来24 分钟前
AI 大模型应用开发前置知识:Python 类型注解全教程
人工智能·python·ai编程
喵手25 分钟前
Python爬虫实战:地图 POI + 行政区反查(合规接口) - 商圈热力数据准备等!
爬虫·python·爬虫实战·零基础python爬虫教学·行政区反查·地图poi·商圈热力数据准备
额,不知道写啥。31 分钟前
P5314 ODT(毒瘤树剖)
数据结构·c++·算法
CHANG_THE_WORLD32 分钟前
深入指针5:回调函数与泛型排序
数据结构·算法
今儿敲了吗37 分钟前
24| 字符串
数据结构·c++·笔记·学习·算法
Wect1 小时前
LeetCode 105. 从前序与中序遍历序列构造二叉树:题解与思路解析
前端·算法·typescript
小雨中_1 小时前
2.9 TRPO 与 PPO:从“信赖域约束”到“近端裁剪”的稳定策略优化
人工智能·python·深度学习·机器学习·自然语言处理