当前位置:网站首页>Alternating merging strings of leetcode simple questions
Alternating merging strings of leetcode simple questions
2022-07-05 22:03:00 【·Starry Sea】
subject
Here are two strings word1 and word2 . Please start from word1 Start , Merging strings by alternately adding letters . If one string is longer than another , Add the extra letters to the end of the merged string .
return Merged string .
Example 1:
Input :word1 = “abc”, word2 = “pqr”
Output :“apbqcr”
explain : The string merge is shown below :
word1: a b c
word2: p q r
After the merger : a p b q c r
Example 2:
Input :word1 = “ab”, word2 = “pqrs”
Output :“apbqrs”
explain : Be careful ,word2 Than word1 Long ,“rs” You need to append to the end of the merged string .
word1: a b
word2: p q r s
After the merger : a p b q r s
Example 3:
Input :word1 = “abcd”, word2 = “pq”
Output :“apbqcd”
explain : Be careful ,word1 Than word2 Long ,“cd” You need to append to the end of the merged string .
word1: a b c d
word2: p q
After the merger : a p b q c d
Tips :
1 <= word1.length, word2.length <= 100
word1 and word2 It's made up of lowercase letters
source : Power button (LeetCode)
Their thinking
Traverse two... Respectively word, Fill in the blank string in the order required by the title , Who grows whose tail is placed at the back .
class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
n1=len(word1)
n2=len(word2)
word1=iter(word1)
word2=iter(word2)
temp=''
if n1==n2:
for i in range(n1):
temp+=next(word1)
temp+=next(word2)
elif n1>n2:
for i in range(n2):
temp+=next(word1)
temp+=next(word2)
for i in range(n1-n2):
temp+=next(word1)
else:
for i in range(n1):
temp+=next(word1)
temp+=next(word2)
for i in range(n2-n1):
temp+=next(word2)
return temp
边栏推荐
- Summary of concurrency control
- A trip to Suzhou during the Dragon Boat Festival holiday
- Livelocks and deadlocks of concurrency control
- 如何向mongoDB中添加新的字段附代码(全)
- AD637使用笔记
- Image editor for their AutoLayout environment
- database mirroring
- Learning of mall permission module
- Blocking protocol for concurrency control
- 如何开发引入小程序插件
猜你喜欢
matlab绘制hsv色轮图
Dbeaver executes multiple insert into error processing at the same time
Two stage locking protocol for concurrency control
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
科技云报道:算力网络,还需跨越几道坎?
Performance monitoring of database tuning solutions
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
随机推荐
Meituan dynamic thread pool practice ideas, open source
Database tuning solution
database mirroring
Recovery technology with checkpoints
Basic grammar of interview (Part 1)
How to add new fields to mongodb with code (all)
Pointer parameter passing vs reference parameter passing vs value parameter passing
Oracle triggers
Defect detection - Halcon surface scratch detection
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Interview questions for basic software testing
Comment développer un plug - in d'applet
每日刷题记录 (十四)
HDU 4391 paint the wall segment tree (water
Learning of mall permission module
Serializability of concurrent scheduling
Performance monitoring of database tuning solutions
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
让开发效率提升的跨端方案
matlab绘制hsv色轮图