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
相关推荐
菜菜小狗的学习笔记6 小时前
剑指Offer算法题(一)数组与矩阵
线性代数·算法·矩阵
仰泳的熊猫6 小时前
题目2269:蓝桥杯2016年第七届真题-冰雹数
开发语言·数据结构·c++·算法·蓝桥杯
冷徹 .6 小时前
2023ICPC山东省赛
c++·算法
Sakinol#6 小时前
Leetcode Hot 100 ——回溯part01
算法·leetcode
一粒马豆6 小时前
如何在二维平面内同时体现系列词汇的词频和相关性?
python·平面·数据可视化·词嵌入·降维·chromadb
罗湖老棍子6 小时前
【例 3】校门外的树(信息学奥赛一本通- P1537)
数据结构·算法·树状数组
guguhaohao6 小时前
平衡二叉树(AVL),咕咕咕!
数据结构·c++·算法
一叶落4386 小时前
LeetCode 137. 只出现一次的数字 II —— 位运算解法
c语言·数据结构·算法·leetcode·哈希算法
JELEE.6 小时前
drf笔记与源码解析
笔记·python·django·drf
阿豪只会阿巴6 小时前
咱这后续安排
c++·人工智能·算法·leetcode·ros2