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
相关推荐
啊我不会诶4 小时前
2024CCPC长春邀请赛
算法
珂朵莉MM4 小时前
第七届全球校园人工智能算法精英大赛-算法巅峰赛产业命题赛第3赛季优化题--启发式算法+操作因子设计
人工智能·算法
CS创新实验室6 小时前
CS实验室行业报告:AI算法工程师就业分析报告
人工智能·算法
7年前端辞职转AI6 小时前
Python 文件操作
python·编程语言
龙文浩_6 小时前
AI梯度下降与PyTorch张量操作技术指南
人工智能·pytorch·python·深度学习·神经网络·机器学习·自然语言处理
呱牛do it6 小时前
企业级绩效考核系统设计与实现:基于FastAPI + Vue3的全栈解决方案
python·fastapi
XiYang-DING6 小时前
【LeetCode】Hash | 136.只出现一次的数字
算法·leetcode·哈希算法
wayz116 小时前
Day 3:逻辑回归与分类预测
算法·分类·逻辑回归
7年前端辞职转AI6 小时前
Python 容器数据类型
python·编程语言
tankeven6 小时前
HJ176 【模板】滑动窗口
c++·算法