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
相关推荐
xzal123 小时前
Python 列表操作笔记:append / extend / + / pop
笔记·python
Doubbbbbbble云3 小时前
图算法中的剪枝技术与启发式优化分析4
算法·机器学习·剪枝
minji...3 小时前
LangGraph-AI智能体开发框架 - LangGraph 入门案例1 : 智能快递配送系统
人工智能·python·ai·langchain·大语言模型·agent·langgraph
鶴哥只手遮天3 小时前
CFL3D深度解析:NASA经典CFD求解器的架构、算法与工程实践
算法
维克兜率天3 小时前
【维克】价差交易实战:配对交易的核心参数设置
大数据·笔记·算法·量化
dtq04243 小时前
数据结构 - 树的概念
c语言·数据结构·学习·算法
晒太羊的猫3 小时前
hot100多维动态规划刷题
算法·动态规划
Είναι η κοπέλα3 小时前
PyTorch 安装与验证
人工智能·pytorch·python
“AI国潮设计-小江”3 小时前
Python实战 | SDXL批量生成“潮汕英歌舞”国潮甜品IP,附核心Prompt与商用授权思路
开发语言·人工智能·python·prompt·aigc
Dream it possible!3 小时前
LeetCode_热题100_知识点总结
算法·leetcode·职场和发展