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
相关推荐
AI Echoes1 天前
LangChain Runnable组件重试与回退机制降低程序错误率
人工智能·python·langchain·prompt·agent
Fleshy数模1 天前
从欠拟合到正则化:用逻辑回归破解信用卡失信检测的召回率困境
算法·机器学习·逻辑回归
im_AMBER1 天前
Leetcode 111 两数相加
javascript·笔记·学习·算法·leetcode
TracyCoder1231 天前
LeetCode Hot100(21/100)——234. 回文链表
算法·leetcode·链表
AAD555888991 天前
YOLO11-Seg+ContextGuided:智能交通流量估算与拥堵检测实战指南
python
可涵不会debug1 天前
Redis魔法学院——第四课:哈希(Hash)深度解析:Field-Value 层级结构、原子性操作与内部编码优化
数据库·redis·算法·缓存·哈希算法
@––––––1 天前
力扣hot100—系列1
算法·leetcode·职场和发展
老鼠只爱大米1 天前
LeetCode经典算法面试题 #236:二叉树的最近公共祖先(RMQ转化、Tarjan离线算法等五种实现方案详细解析)
算法·leetcode·二叉树·lca·并查集·最近公共祖先·rmq
问好眼1 天前
【信息学奥赛一本通】1296:开餐馆
c++·算法·动态规划·信息学奥赛
rose and war1 天前
python和jinja版本问题导致的访问报500
python·ios