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
相关推荐
汽车仪器仪表相关领域7 分钟前
经典指针+瞬态追踪:MTX-A模拟废气温度(EGT)计 改装/赛车/柴油车排气温度监测实战全解
大数据·功能测试·算法·机器学习·可用性测试
yufuu9812 分钟前
Python在金融科技(FinTech)中的应用
jvm·数据库·python
如果你想拥有什么先让自己配得上拥有14 分钟前
斐波那契黄金分割自然界演化以及金融上的共振?
算法·金融
灰色小旋风16 分钟前
力扣第1题:两数之和(C++)
c++·算法
电饭叔18 分钟前
GUI by Python 6 一段 gui 代码分析
开发语言·python
OnYoung18 分钟前
持续集成/持续部署(CI/CD) for Python
jvm·数据库·python
2301_8223776518 分钟前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
民乐团扒谱机18 分钟前
机器学习 第二弹 和AI斗智斗勇 机器学习核心知识点全解析(GBDT/XGBoost/LightGBM/随机森林+调参方法)
算法·决策树·机器学习
u01092727119 分钟前
用Python和Twilio构建短信通知系统
jvm·数据库·python
智驱力人工智能21 分钟前
实线变道检测 高架道路安全治理的工程化实践 隧道压实线监测方案 城市快速路压实线实时预警 压实线与车牌识别联动方案
人工智能·opencv·算法·安全·yolo·边缘计算