Greatest Common Divisor of Strings - Leetcode 1071 - Python

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
Solving Greatest Common Divisor of Strings Leetcode 1071, today's daily leetcode problem on January 31st.
🥷 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/greates...
0:00 - Read the problem
1:10 - Drawing Explanation
5:32 - Coding Explanation
leetcode 1071
#neetcode #leetcode #python

Пікірлер: 45

  • @hithambasheir3283
    @hithambasheir3283 Жыл бұрын

    I was stuck on this problem and it literally took me the first 3 minutes to understand where I'm blocked, paused the video and successfully solved it 🥳

  • @girishnakate5014
    @girishnakate5014 Жыл бұрын

    I had asked for more easy problems and here you are. Thank you.

  • @NeetCodeIO

    @NeetCodeIO

    Жыл бұрын

    Well normally I just do the daily LC problems, so difficulty is usually out of my control 😅

  • @def__init
    @def__init Жыл бұрын

    Maybe it’s just late but I feel like I’ve seen easier mediums than this

  • @NeetCodeIO

    @NeetCodeIO

    Жыл бұрын

    Yeah, this is definitely difficult for an easy

  • @Snoresville
    @Snoresville Жыл бұрын

    there is an insane solution that involves knowing that there is a GCD of strings if str1 + str2 == str2 + str1

  • @zeta_meow_meow

    @zeta_meow_meow

    Жыл бұрын

    damn bro , discuss section is surely amazing

  • @axelstahl4386

    @axelstahl4386

    Жыл бұрын

    It’s because they all have to be made up of the same stuff. Like aa + a=a+aa cause it’s just a repeated. But b + a != a + b because it’s not one thing repeated, or a factor

  • @sheeniebeanie2597

    @sheeniebeanie2597

    Жыл бұрын

    class Solution: def gcdOfStrings(self, str1: str, str2: str) -> str: if str1 + str2 == str2 + str1: x = gcd(len(str1),len(str2)) return str1[:x] else: return ""

  • @zaidnissar356

    @zaidnissar356

    7 ай бұрын

    Holy frick that is an insane solution. My mind is blown!!

  • @iameenr

    @iameenr

    4 ай бұрын

    ​@@sheeniebeanie2597Phenomal 😮 How can one come up with such ideas!

  • @BRBallin1
    @BRBallin14 ай бұрын

    That modulo operation to rule out the substring sizing is such a good pointer for optimizing the solution

  • @jesuscruz8008
    @jesuscruz8008 Жыл бұрын

    This video helped explain it so well glad i watched

  • @podilichaitanyaakhilkumar4911
    @podilichaitanyaakhilkumar4911 Жыл бұрын

    Keep uploading more leetcode problems as well as system design.

  • @agreen254
    @agreen25410 ай бұрын

    divmod() is a nice method for your isDivisor() function. It returns both the integer division result and the remainder as a tuple. divmod(5, 2) = (2, 1)

  • @kirillzlobin7135
    @kirillzlobin713510 ай бұрын

    Awesome quality content

  • @kuijaye
    @kuijaye Жыл бұрын

    How is this easy. I'm starting leetcode and the first 2 so far was a bit hard

  • @indie_gem
    @indie_gem Жыл бұрын

    how about if we use ascii to find the gcd?

  • @KeyanSong-br5li
    @KeyanSong-br5li4 ай бұрын

    Thank u!

  • @lassekock4315
    @lassekock431510 ай бұрын

    Correct me if I'm wrong, but in line 9, minute 8:39 it does not work in bot cases as you described. Behind the "and" (str1[:l] * f1 == str1 and str2[:l] * f2 == str2) we need to compare str2 to the multiple of str1 because otherwise it is not checked if the actual letters of the substring are the same in str1 and str2. So this is the correct way to do it: str1[:l] * f1 == str1 and str1[:l] * f2 == str2

  • @mnaresh3382

    @mnaresh3382

    5 ай бұрын

    I was about to say that too.

  • @lamborghinicentenario2497
    @lamborghinicentenario24975 ай бұрын

    I found this solution that works, unlike the solution in this video (which doesn't seem to solve all test cases on my end): class Solution(object): def gcdOfStrings(self, str1, str2): if str1 + str2 != str2 + str1: return "" def gcd(a, b): while b: a, b = b, a % b return a return str1[:gcd(len(str1), len(str2))]

  • @kingkidc2844

    @kingkidc2844

    7 күн бұрын

    The solution in this video isn't right for all cases return str1[:l] * p == str1 and str2[:l] * q == str2 this code should be switched to return str1[:l] * p == str1 and str1[:l] * q == str2 so it compares the substring str1 with str2

  • @krkode8765
    @krkode876510 ай бұрын

    im confused on the big o explaination could someone explain it? i would have thought itd just be m cause isDivisor is just refactored out of the main loop its not like a smaller loop right.

  • @error-my9ut
    @error-my9ut11 ай бұрын

    i thought of this approach was thinking why no one did this instead of str1+str2 ,like every soln has that code only idk why?

  • @twezotoons
    @twezotoons2 күн бұрын

    this isn't easy, I used recursion. But thank you for the explanation just so I can see how others solve it. I know eventually this will be easier for me.

  • @tranminhquang4541
    @tranminhquang45414 ай бұрын

    lol this is more on the medium side

  • @rashzh5502
    @rashzh5502 Жыл бұрын

    🔥🔥🔥

  • @jay5929
    @jay5929 Жыл бұрын

    There is a linear solution, based on the fact that str1 and str2 have a GCD string if and only if str1 + str2 == str2 + str1. But I fail to find a solution that explains why the right-to-left direction is true.

  • @sheeniebeanie2597

    @sheeniebeanie2597

    Жыл бұрын

    i made the linear solution--it worked in leetcode but dk if it works generally

  • @omaryahia
    @omaryahia23 күн бұрын

    wow, nice

  • @zeta_meow_meow
    @zeta_meow_meow Жыл бұрын

    not that easy i guess 😅

  • @bernardwodoame9850
    @bernardwodoame98506 ай бұрын

    I think I have to watch it again... I got lost .

  • @bandhammanikanta
    @bandhammanikantaАй бұрын

    Neetcode and NeetcodeIO are two different account?

  • @ramyhussein4091
    @ramyhussein409112 күн бұрын

    An easier solution: def gcdOfStrings( str1, str2): if str1 + str2 != str2 + str1: return "" # initializa the maximum possible length of the GCD max_length = min(len(str1), len(str2)) for length in range(max_length, 0, -1): if len(str1) % length == 0 and len(str2) % length == 0: return str1[:length] return ""

  • @lamborghinicentenario2497
    @lamborghinicentenario24975 ай бұрын

    This code only passes 21 out of 123 test cases on leetcode

  • @noaht9184
    @noaht91846 сағат бұрын

    This should be a medium bruh

  • @sagark4080
    @sagark40804 ай бұрын

    Time Complexity of below soln: O(logn)

  • @prerittameta
    @prerittameta8 ай бұрын

    Please correct the code as it would fail for str1 = 'ABCD' str2 = 'DEFH' the correction in line 9 should be as below return str1[:l]*f1 == str1 and str2[:l]*f2 == str2 and str1[:l] == str2[:l]

  • @hi-io

    @hi-io

    7 ай бұрын

    It seems to me that it might not always work out, because str1 and str2 do not always follow the same order

  • @effortlessjapanese123

    @effortlessjapanese123

    2 ай бұрын

    hey can you explain why we need to add str1[:l] == str2[:l]?

  • @kelvinjohndomeh1488
    @kelvinjohndomeh14889 ай бұрын

    How is this easy at the first place..naaaa

  • @omaralzakout4100
    @omaralzakout410011 ай бұрын

    Bro, there is a more efficient solution that takes 2 lines of code in c++. Suppose the first string is s and the second is t then: string gcdOfStrings(string s, string t) { if(s+t != t+s) return ""; return s.substr(0, __gcd((int)s.size(), (int)t.size())); }

  • @samuelbanya
    @samuelbanya Жыл бұрын

    4:14 Man, what am I even looking at? It's not even psuedo math that makes any sense whatsoever. If you had put something like 6 = 2x, it's basic algebra sure. But wow, that is an awful explanation in comparison, and I would suggest maybe redoing this video's section to be more clear. The problem itself is the worst word salad I have ever read in my entire life, but this solution is even more convoluted.