Merge Strings Alternately - Leetcode 1768 - Python

  Рет қаралды 18,280

NeetCodeIO

NeetCodeIO

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🥷 Discord: / discord
🐦 Twitter: / neetcode1
🐮 Support the channel: / neetcode
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
Problem Link: leetcode.com/problems/merge-s...
0:00 - Read the problem
0:30 - Drawing Explanation
2:00 - Coding Explanation
leetcode 1768
#neetcode #leetcode #python

Пікірлер: 29
@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?
@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 2 ай бұрын
could you explain ?
@shubhambiswas3723
@shubhambiswas3723 Жыл бұрын
go for the word with shorter length to loop and then concat what is left on the longer word
@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
@NeoGame1000
@NeoGame1000 6 ай бұрын
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 5 ай бұрын
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)).
@Nisha.......
@Nisha....... Жыл бұрын
Leetcode problem number 2096 please!!!
@bort-oy6ux
@bort-oy6ux 4 ай бұрын
i solved it with a stringbuilder in java idk if that's a correct approach
@phongtranquoc7557
@phongtranquoc7557 7 күн бұрын
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
@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 😉
@ibnjay7
@ibnjay7 7 ай бұрын
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 6 ай бұрын
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
@adhivp5594
@adhivp5594 2 ай бұрын
Can anybody explain why converting string to list and then converting list to string is more efficient in python.
@adama7752
@adama7752 2 ай бұрын
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 2 ай бұрын
@@adama7752 thanks
@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
@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
Рет қаралды 6 М.
Greatest Common Divisor of Strings - Leetcode 1071 - Python
9:16
СНЕЖКИ ЛЕТОМ?? #shorts
00:30
Паша Осадчий
Рет қаралды 7 МЛН
Countries Treat the Heart of Palestine #countryballs
00:13
CountryZ
Рет қаралды 28 МЛН
🍟Best French Fries Homemade #cooking #shorts
00:42
BANKII
Рет қаралды 64 МЛН
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 262 М.
Find Peak Element - Leetcode 162 - Python
11:02
NeetCodeIO
Рет қаралды 31 М.
Merge Strings Alternately - LeetCode 1768 - Python Solution
5:26
Data with Carter
Рет қаралды 1,3 М.
A Simple Kafka and Python Walkthrough
11:34
Quix
Рет қаралды 9 М.
This video will change the way you think when coding
7:59
Sahil & Sarra
Рет қаралды 103 М.
Design Hashmap - Leetcode 706 - Python
14:30
NeetCodeIO
Рет қаралды 26 М.
Accounts Merge - Leetcode 721 - Python
17:13
NeetCodeIO
Рет қаралды 22 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 601 М.
The unfair way I got good at Leetcode
6:47
Dave Burji
Рет қаралды 373 М.
СНЕЖКИ ЛЕТОМ?? #shorts
00:30
Паша Осадчий
Рет қаралды 7 МЛН