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
相关推荐
折哥的程序人生 · 物流技术专研6 小时前
Java面试85题图解版 · 特别篇:2026后端高频面试题复盘(算法底层逻辑+高并发架构设计全解析,附Java实战代码)
java·网络·数据库·算法·面试
xxie1237946 小时前
return与print
开发语言·python
秋96 小时前
从 Python 后端工程师转型 AI Engineer(AI 工程化)的完整补课清单(2026实战版)
开发语言·人工智能·python
慕木沐7 小时前
Google ADK Java 1.0版本 核心机制与实战 Demo
java·开发语言·python
想吃火锅10057 小时前
【leetcode】14.最长公共前缀js
算法·leetcode·职场和发展
Tbisnic7 小时前
AI大模型学习第十一天:技术选型、安全防护与金融实战
python·学习·ai·大模型·提示词工程
hboot7 小时前
AI工程师第一课 - Python
前端·后端·python
云絮.8 小时前
数据库操作
数据库·mysql·算法·oracle
许彰午8 小时前
30_Java Stream流操作全解
java·windows·python
小林ixn8 小时前
LeetCode 206. 反转链表(迭代 + 递归详解)
算法·leetcode·链表