Merge Strings Alternately - Leetcode 1768 - Python

preview_player
Показать описание


0:00 - Read the problem
0:30 - Drawing Explanation
2:00 - Coding Explanation

leetcode 1768

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

Was asked a LC hard in an interview today that you covered.
Hoping the rest of the rounds go well, so thankful for the content.

def__init
Автор

This was a simple and easy to understand solution. Thanks!

helloworldcsofficial
Автор

Would love and appreciate a video on all the basics concepts of data structures and algorithms and there uses. Would help lot of non cs students 🙏🙏
Edited: I think u already hav such videos like big O for coding interviews
Need that kind of videos for DSA

jagannthaja
Автор

This was my solution

class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
n1 = 0
n2 = 0
res = ''

while n1 < len(word1) or n2 < len(word2):

if n1 > len(word2) - 1:
res += word1[n1]
n1 += 1
continue
if n2 > len(word1) - 1:
res += word2[n2]
n2 += 1
continue

res += word1[n1] + word2[n2]
n1 += 1
n2 += 1

return res

abdullahqureshi
Автор

Great video as always,
I think One pointer will do the job we don't need two

m.kamalali
Автор

go for the word with shorter length to loop and then concat what is left on the longer word

shubhambiswas
Автор

another approach with time complexity O(m+n): "class Solution {
public String mergeAlternately(String word1, String word2) {
String ans = "";
int length1 = word1.length();
int length2 = word2.length();
int index = 0;
for(int i = 0; i < length1; i++) {
ans += word1.charAt(i);
if(i == index && i <= length1 - 1 && index <= length2 - 1) {
ans += word2.charAt(index);
index++;
}
}

if(length1 < length2) {
ans += word2.substring(index, length2);
}
return ans;
}
}"

phongtranquoc
Автор

Hi NeetCode, could you please explain why did you assess the time complexity as O(n + m), and not as O(Max(n, m). Because basically the algorithm will loop at max Math.Max(n, m) if we are using while with two conditions.

NeoGame
Автор

class Solution:
def mergeAlternately(self, word1: str, word2: str) -> str:
output = ""
len1 = len(word1)
len2 = len(word2)
pointer1 = 0
pointer2 = 0
for i in range(len1+len2):
if pointer1 < len1:
output = output + word1[pointer1]
pointer1 = pointer1+1
if pointer2 < len2:
output = output + word2[pointer2]
pointer2 = pointer2 +1
return output

I did it such an unpythonic way

ibnjay
Автор

i solved it with a stringbuilder in java idk if that's a correct approach

bort-oyux
Автор

Try pre sizing the list. Bet that speeds it up a fair bit. Also one index.

Better yet use c++, malloc the size of memory you need(+1 for the null terminator), the set the char at each memory address and return a const char* that points to the start of the string

RockuHD
Автор

Hey Neetcode, Can you please solve this problem 662. Maximum Width of Binary Tree?

marwanahmed
Автор

Why are you using two i and j counters when they are exactly the same? j can be removed completely and be replaced by i

redone
Автор

Leetcode problem number 2096 please!!!

Nisha.......
Автор

Can anybody explain why converting string to list and then converting list to string is more efficient in python.

adhivp
Автор

I have a question. Do Google know it is you(from your voice and such)

infinitygod