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
相关推荐
曦月逸霜3 小时前
啥是RAG 它能干什么?
人工智能·python·机器学习
浅念-3 小时前
递归解题指南:LeetCode经典题全解析
数据结构·算法·leetcode·职场和发展·排序算法·深度优先·递归
2301_769340673 小时前
如何在 Vuetify 中可靠捕获 Chip 关闭事件(包括键盘触发).txt
jvm·数据库·python
Kiling_07044 小时前
Java集合进阶:Set与Collections详解
算法·哈希算法
智者知已应修善业4 小时前
【51单片机89C51及74LS273、74LS244组成】2022-5-28
c++·经验分享·笔记·算法·51单片机
洛水水4 小时前
【力扣100题】33.验证二叉搜索树
算法·leetcode·职场和发展
SimpleLearingAI4 小时前
聚类算法详解
算法·数据挖掘·聚类
南 阳5 小时前
Python从入门到精通day66
开发语言·python
m0_596749095 小时前
JavaScript中手动实现一个new操作符的底层逻辑
jvm·数据库·python
刀法如飞5 小时前
Go 字符串查找的 20 种实现方式,用不同思路解决问题
算法·面试·程序员