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
相关推荐
Empty_7771 小时前
编程之python基础
开发语言·python
Miraitowa_cheems2 小时前
LeetCode算法日记 - Day 73: 最小路径和、地下城游戏
数据结构·算法·leetcode·职场和发展·深度优先·动态规划·推荐算法
野蛮人6号2 小时前
力扣热题100道之560和位K的子数组
数据结构·算法·leetcode
Swift社区3 小时前
LeetCode 400 - 第 N 位数字
算法·leetcode·职场和发展
fengfuyao9853 小时前
BCH码编译码仿真与误码率性能分析
算法
哲Zheᗜe༘4 小时前
了解学习Python编程之python基础
开发语言·python·学习
小白不想白a4 小时前
每日手撕算法--哈希映射/链表存储数求和
数据结构·算法
剪一朵云爱着4 小时前
力扣2080. 区间内查询数字的频率
算法·leetcode
落日漫游4 小时前
数据结构笔试核心考点
java·开发语言·算法
Dream it possible!4 小时前
LeetCode 面试经典 150_栈_有效的括号(52_20_C++_简单)(栈+哈希表)
c++·leetcode·面试··哈希表