当前位置:网站首页>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
边栏推荐
- AD637 usage notes
- Installation of VMware Workstation
- K210学习笔记(四) K210同时运行多个模型
- AD637使用笔记
- Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
- Talking about MySQL index
- 让开发效率提升的跨端方案
- Storage optimization of performance tuning methodology
- Analyse des risques liés aux liaisons de microservices
- Business learning of mall order module
猜你喜欢
The real situation of programmers
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
Oracle hint understanding
Granularity of blocking of concurrency control
华为快游戏调用登录接口失败,返回错误码 -1
Common interview questions of redis factory
Summary of concurrency control
Performance monitoring of database tuning solutions
Stored procedures and stored functions
Overview of database recovery
随机推荐
华为云ModelArts文本分类–外卖评论
How to use tensorflow2 for cat and dog classification and recognition
Poj3414广泛搜索
POJ 3237 tree (tree chain splitting)
The real situation of programmers
How can Huawei online match improve the success rate of player matching
Serializability of concurrent scheduling
Win11运行cmd提示“请求的操作需要提升”的解决方法
Interview questions for basic software testing
装饰器学习01
Installation of VMware Workstation
Basic grammar of interview (Part 1)
Huawei game multimedia service calls the method of shielding the voice of the specified player, and the error code 3010 is returned
Oracle triggers
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
Defect detection - Halcon surface scratch detection
Create a virtual machine on VMware (system not installed)
科技云报道:算力网络,还需跨越几道坎?
regular expression
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!