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 分钟前
软件测试之黑盒测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
_深海凉_11 分钟前
LeetCode热题100-合并两个有序链表
算法·leetcode·链表
人工智能培训13 分钟前
样本效率与安全探索的矛盾解析及平衡路径
大数据·人工智能·深度学习·算法·机器学习·知识图谱·故障诊断
第一程序员13 分钟前
Python深度学习实战:从理论到应用
python·github
yoso14 分钟前
Claude Code 源码架构深度解析:1884 个文件背后的 AI 编程工具设计哲学
算法·架构
第二只羽毛16 分钟前
C++ 高并发内存池4
java·大数据·linux·c++·算法
乐园游梦记18 分钟前
下载 Docker 镜像(CVAT)资源
人工智能·python·深度学习·yolo·机器学习·cvat
散峰而望19 分钟前
【数据结构】并查集从入门到精通:基础实现、路径压缩、扩展域、带权,一网打尽
数据结构·c++·算法·github·剪枝·推荐算法
第一程序员21 分钟前
Python数据分析:Pandas vs Polars 详细对比
python·github
程序员杰哥22 分钟前
Web UI自动化测试之PO篇
自动化测试·软件测试·python·selenium·测试工具·ui·测试用例