当前位置:网站首页>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

边栏推荐
- 微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
- Database tuning solution
- Granularity of blocking of concurrency control
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- AD637使用笔记
- Analyse des risques liés aux liaisons de microservices
- Pointer parameter passing vs reference parameter passing vs value parameter passing
- About the writing method of SQL field "this includes" and "included in" strings
- A long's perception
- Talking about MySQL index
猜你喜欢

Performance monitoring of database tuning solutions

A trip to Suzhou during the Dragon Boat Festival holiday

Learning of mall permission module

数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖

U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程

Sentinel production environment practice (I)

K210学习笔记(四) K210同时运行多个模型

华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010

QML reported an error expected token ";", expected a qualified name ID

Serializability of concurrent scheduling
随机推荐
HDU 4391 paint the wall segment tree (water
Type of fault
Kingbasees v8r3 data security case - audit record clearing case
Oracle advanced query
Business learning of mall commodity module
Summary of concurrency control
Serializability of concurrent scheduling
阿龙的感悟
HYSBZ 2243 染色 (树链拆分)
About the writing method of SQL field "this includes" and "included in" strings
How to develop and introduce applet plug-ins
Oracle HugePages没有被使用导致服务器很卡的解决方法
ICMP introduction
Learning of mall permission module
Database tuning solution
Win11运行cmd提示“请求的操作需要提升”的解决方法
854. String BFS with similarity K
poj 3237 Tree(樹鏈拆分)
Concurrency control of performance tuning methodology
Oracle hint understanding