当前位置:网站首页>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
边栏推荐
- Granularity of blocking of concurrency control
- Detailed explanation of memset() function usage
- The American Championship is about to start. Are you ready?
- Two stage locking protocol for concurrency control
- Oracle hint understanding
- Web3为互联网带来了哪些改变?
- AD637 usage notes
- Interview questions for basic software testing
- Drawing HSV color wheel with MATLAB
- 科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
猜你喜欢
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
Granularity of blocking of concurrency control
Official clarification statement of Jihu company
笔记本电脑蓝牙怎么用来连接耳机
Performance monitoring of database tuning solutions
华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
装饰器学习01
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Type of fault
随机推荐
Recovery technology with checkpoints
Shell script, awk uses if, for process control
Detailed explanation of memset() function usage
CRM creates its own custom report based on fetch
How to add new fields to mongodb with code (all)
装饰器学习01
总结出现2xx、3xx、4xx、5xx状态码的原因
ICMP introduction
资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
Overview of concurrency control
Web3为互联网带来了哪些改变?
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
oracle 控制文件的多路复用
Decorator learning 01
科技云报道:算力网络,还需跨越几道坎?
ICMP 介绍
PyGame practical project: write Snake games with 300 lines of code
初级软件测试必问面试题
PIP install beatifulsoup4 installation failed
NET中小型企业项目开发框架系列(一个)