Merge Strings Alternately - Leetcode 1768 - Python

  Рет қаралды 28,586

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 38
@def__init
@def__init Жыл бұрын
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.
@stcattc
@stcattc Жыл бұрын
Wc lc number?
@heyyayesh
@heyyayesh Жыл бұрын
Can you say which one?
@mirkelor
@mirkelor Жыл бұрын
which one?
@gabe5225
@gabe5225 17 күн бұрын
@@stcattc 1768
@jagannthaja7904
@jagannthaja7904 Жыл бұрын
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
@m.kamalali
@m.kamalali Жыл бұрын
Great video as always , I think One pointer will do the job we don't need two
@dineshsenthil1237
@dineshsenthil1237 9 ай бұрын
could you explain ?
@t.r.r901
@t.r.r901 5 ай бұрын
@@dineshsenthil1237 I know I'm a tad late but he means (I think) having one pointer that goes throughs and appends each element to the answer. I did it this way but its complexity isn't great. Here's my code if that helps explaining at all :) def mergeAlternately(self, word1, word2): p=0 ans = [] len1, len2 = len(word1), len(word2) while p < len1 or p < len2: if p < len1: ans.append(word1[p]) if p < len2: ans.append(word2[p]) p += 1 return ''.join(ans)
@smitpatel7930
@smitpatel7930 23 күн бұрын
True Buddy Right now I tried and it worked fine class Solution(object): def mergeAlternately(self, word1, word2): """ :type word1: str :type word2: str :rtype: str """ i=0 finalstring=[] while i
@johnpulawski35
@johnpulawski35 5 күн бұрын
yah you can do one pointer, add one character from word1 and one character from word2, then just append the remainder of both strings after looping (if no remainder nothing gets added)
@helloworldcsofficial
@helloworldcsofficial 2 ай бұрын
This was a simple and easy to understand solution. Thanks!
@abdullahqureshi7165
@abdullahqureshi7165 4 ай бұрын
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
@NeoGame1000
@NeoGame1000 Жыл бұрын
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.
@S3aw0lf
@S3aw0lf Жыл бұрын
In this case we are iterating over both the strings atleast once in our while loop because we need all the characters from both strings combined.If we were asked to append only the alternating characters for eg. a="abcd", b="pqr" and result = "aqcd" then it would have been O(Max(m,n)).
@shubhambiswas3723
@shubhambiswas3723 Жыл бұрын
go for the word with shorter length to loop and then concat what is left on the longer word
@samuelogunwale2483
@samuelogunwale2483 5 ай бұрын
This is exactly what I did
@ibnjay7
@ibnjay7 Жыл бұрын
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
@hawksawed
@hawksawed Жыл бұрын
Same. But I think this may be a slightly more efficient version of yours: onesize = len(word1) twosize = len(word2) maxsize = max(onesize, twosize) x = "" for i in range(maxsize): if i < onesize: x += word1[i] if i < twosize: x += word2[i] return x
@phongtranquoc7557
@phongtranquoc7557 6 ай бұрын
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
@redone729
@redone729 6 ай бұрын
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
@adhivp5594
@adhivp5594 9 ай бұрын
Can anybody explain why converting string to list and then converting list to string is more efficient in python.
@adama7752
@adama7752 9 ай бұрын
In python strings are immutable (cannot be changed), instead a new string is created. So as you append a char, python copied your old string into a new string with 1 extra space where your char is appended. When you join a list I'm assuming it's a built-in function that will allocate memory required for the whole list.
@adhivp5594
@adhivp5594 9 ай бұрын
@@adama7752 thanks
@bort-oy6ux
@bort-oy6ux 10 ай бұрын
i solved it with a stringbuilder in java idk if that's a correct approach
@marwanahmed2657
@marwanahmed2657 Жыл бұрын
Hey Neetcode, Can you please solve this problem 662. Maximum Width of Binary Tree?
@NeetCodeIO
@NeetCodeIO Жыл бұрын
How'd you know that was gonna be todays daily problem 😉
@RockuHD
@RockuHD Жыл бұрын
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
@Nisha.......
@Nisha....... Жыл бұрын
Leetcode problem number 2096 please!!!
@aribasiebel
@aribasiebel Жыл бұрын
Do we need both i and j ?
@patchouli9
@patchouli9 Жыл бұрын
You can use a single i and take from both strings at the same index, starting with word1, then at the end compare the lengths of the strings and take the rest of the longer one. The i,j way is done is similar to a Linked List problem where you would pick from two LL into a single LL and just concat at the end. I think the i,j way is less error prone and is the way I prefer doing it, also easily applicable to various LL problems.
@nikhil_a01
@nikhil_a01 Жыл бұрын
You can do it with a single pointer as @patchouli said. I also prefer to use i and j though. I think it's clearer and it's a more general pattern. But if you do use a single pointer, you don't need to compare the lengths of the strings at the end. You can still use the slicing. The code is below. (And before anyone guesses that this won't work, yes I did submit this). class Solution: def mergeAlternately(self, word1: str, word2: str) -> str: i = 0 res = [] while i < len(word1) and i < len(word2): res.append(word1[i]) res.append(word2[i]) i += 1 res.append(word1[i:]) res.append(word2[i:]) return "".join(res)
@utkarshdewan8736
@utkarshdewan8736 Жыл бұрын
@@patchouli9 i did the LL way where I used 2 while loops to exhaust the remaining string
@808brotherstv4
@808brotherstv4 5 күн бұрын
Anyone tried this in java with Stringbuilder and substring
@infinitygod5379
@infinitygod5379 Жыл бұрын
I have a question. Do Google know it is you(from your voice and such)
@vishnuvardhan2687
@vishnuvardhan2687 Жыл бұрын
Aliens 👽👽 attendance taken by here
@hypermeero4782
@hypermeero4782 Жыл бұрын
first
Sign of An Array - Leetcode 1822 - Python
8:30
NeetCodeIO
Рет қаралды 7 М.
What Is Dynamic Programming and How To Use It
14:28
CS Dojo
Рет қаралды 1,6 МЛН
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН
How to Start Leetcode (as a beginner)
8:45
Ashish Pratap Singh
Рет қаралды 1 МЛН
70% of Programmers can't solve this LeetCode Easy Question
7:32
Greatest Common Divisor of Strings | Leetcode - 1071 | Python
10:10
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 905 М.
LeetCode 1768. Merging Strings Alternately (Java)
5:13
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 777 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 713 М.
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 146 М.
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН