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
相关推荐
Root_Smile9 小时前
【Python】pip freeze用法
开发语言·python·pip
qq_12498707539 小时前
基于Hadoop的黑龙江旅游景点推荐系统的设计与实现(源码+论文+部署+安装)
大数据·hadoop·分布式·python·信息可视化
gihigo19989 小时前
LDPC码硬判决译码算法的详细解析
网络·算法
Clarence Liu9 小时前
快慢指针问题
后端·算法
Swift社区9 小时前
LeetCode 467 环绕字符串中唯一的子字符串
算法·leetcode·职场和发展
Yzzz-F9 小时前
P3870 [TJOI2009] 开关[线段树(区间加 区间和 变种)]
算法
不过如此19519 小时前
Jira系统中JQL语句的介绍
python·sql·jira
wen__xvn9 小时前
代码随想录算法训练营DAY7第三章 哈希表part02
数据结构·算法·散列表
代码游侠9 小时前
应用——Linux Framebuffer 图形库显示
linux·运维·服务器·数据库·笔记·算法
不过如此19519 小时前
Python操作Jira实现不同项目之间的Issue同步
python·jira·issue