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

边栏推荐
猜你喜欢

【愚公系列】2022年7月 Go教学课程 004-Go代码注释

Interprocess communication in the "Chris Richardson microservice series" microservice architecture

Implementation technology of recovery

华为快游戏调用登录接口失败,返回错误码 -1

Matlab | app designer · I used Matlab to make a real-time editor of latex formula

Talking about MySQL index

Summary of concurrency control

Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer

Meituan dynamic thread pool practice ideas, open source

Deeply convinced plan X - network protocol basic DNS
随机推荐
854. String BFS with similarity K
A trip to Suzhou during the Dragon Boat Festival holiday
Drawing HSV color wheel with MATLAB
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
Huawei game multimedia service calls the method of shielding the voice of the specified player, and the error code 3010 is returned
Stored procedures and stored functions
MMAP learning
HYSBZ 2243 染色 (树链拆分)
Win11运行cmd提示“请求的操作需要提升”的解决方法
Efficiency difference between row first and column first traversal of mat data types in opencv
Official clarification statement of Jihu company
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Huawei cloud modelarts text classification - takeout comments
Create a virtual machine on VMware (system not installed)
Installation of VMware Workstation
How to organize an actual attack and defense drill
Pl/sql basic case
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
Two stage locking protocol for concurrency control
Livelocks and deadlocks of concurrency control