当前位置:网站首页>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代码注释
- The real situation of programmers
- Create a virtual machine on VMware (system not installed)
- 如何组织一场实战攻防演练
- Livelocks and deadlocks of concurrency control
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- 大约SQL现场“这包括”与“包括在”字符串的写法
- Daily question brushing record (XIV)
- EBS Oracle 11g cloning steps (single node)
- 科技云报道:算力网络,还需跨越几道坎?
猜你喜欢
Analysis and test of ModbusRTU communication protocol
华为快游戏调用登录接口失败,返回错误码 -1
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
元宇宙中的三大“派系”
Yolov5 training custom data set (pycharm ultra detailed version)
笔记本电脑蓝牙怎么用来连接耳机
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
Huawei game multimedia service calls the method of shielding the voice of the specified player, and the error code 3010 is returned
Experienced inductance manufacturers tell you what makes the inductance noisy. Inductance noise is a common inductance fault. If the used inductance makes noise, you don't have to worry. You just need
随机推荐
EBS Oracle 11g cloning steps (single node)
HDU 4391 Paint The Wall 段树(水
Oracle hint understanding
Codeforces 12D Ball 树形阵列模拟3排序元素
About the writing method of SQL field "this includes" and "included in" strings
Oracle triggers
ICMP introduction
Drawing HSV color wheel with MATLAB
Pl/sql basic case
微服務鏈路風險分析
Defect detection - Halcon surface scratch detection
Huawei fast game failed to call the login interface, and returned error code -1
Detailed explanation of memset() function usage
Codeforces 12D ball tree array simulation 3 sorting elements
Bitbucket installation configuration
Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
CRM creates its own custom report based on fetch
Meituan dynamic thread pool practice ideas, open source
Decorator learning 01
Daily question brushing record (XIV)