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
相关推荐
凌波粒12 分钟前
LeetCode--704.二分查找(数组)
算法·leetcode·职场和发展
B站计算机毕业设计之家13 分钟前
Python 基于协同过滤的动漫推荐与数据分析平台 Django框架 协同过滤推荐算法 可视化 数据分析 大数据 大模型 计算机毕业设计(建议收藏)✅
大数据·python·scrapy·数据分析·django·课程设计·推荐算法
xiaoye-duck13 分钟前
《算法题讲解指南:动态规划算法--路径问题》--11.按摩师,12.打家劫舍II
c++·算法·动态规划
阿贵---16 分钟前
构建一个基于命令行的待办事项应用
jvm·数据库·python
紫丁香19 分钟前
pytest_自动化测试4
python·功能测试·单元测试·集成测试·pytest
代码探秘者20 分钟前
【算法篇】1.双指针
java·数据结构·人工智能·后端·python·算法
qq_4176950523 分钟前
C++中的中介者模式
开发语言·c++·算法
Rolei_zl27 分钟前
AIGC(生成式AI)试用 48 -- AI与软件开发过程3
python·aigc
qq_4160187229 分钟前
持续集成/持续部署(CI/CD) for Python
jvm·数据库·python
qq_4160187232 分钟前
使用Python处理计算机图形学(PIL/Pillow)
jvm·数据库·python