Isomorphic Strings Leetcode Solution

Sdílet
Vložit
  • čas přidán 19. 10. 2020
  • Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
    NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we discuss the Isomorphic Strings problem using hashmaps in java. In this problem,
    1. You are given two strings s1 and s2.
    2. You have to determine if they are isomorphic or not.
    3. Two strings are called isomorphic if -
    - Every character of s1 can be mapped with characters of s2.
    - A character of s1 can be mapped to only one character.
    - All occurrences of a character must be replaced with another character while preserving the order of characters.
    Note - Length of s1 is equal to the length of s2.
    To attempt and to submit this problem, click here: www.pepcoding.com/resources/d...
    For a better experience and more exercises, VISIT: www.pepcoding.com/resources/o...
    #hashmaps #datastructure #algorithms
    Have a look at our result: www.pepcoding.com/placements
    Follow us on our FB page: / pepcoding
    Follow us on Instagram: / pepcoding
    Follow us on LinkedIn: / pepcoding-education

Komentáře • 81

  • @Rahul0508-n9m
    @Rahul0508-n9m Před 3 lety +22

    who are these people who disliked very simple explanation. Thank you for the brilliant explanation. 🙏

    • @Pepcoding
      @Pepcoding  Před 3 lety +9

      The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
      So, keep motivating and Keep loving😊

    • @Rahul0508-n9m
      @Rahul0508-n9m Před 3 lety +9

      @@Pepcoding Yes not even single video, whenever i search any video and if i find you've given explanation on that, dusri video open karne ka sawal hi nahi hota.
      👌🏻

    • @aksharbrahm4618
      @aksharbrahm4618 Před 5 měsíci

      @Pepcoding I also agree with@@Rahul0508-n9m . I really appreciate your explanation.

  • @sudarshanmaskare3328
    @sudarshanmaskare3328 Před 3 lety +1

    Best explanation,covering all edge cases

  • @anshuakansha2976
    @anshuakansha2976 Před 3 lety

    how in line 26 map2.put(ch2, true) is working it should be ch1 in place of true otherwise it will show error of boolean cannot be converted to Character

  • @ankitagarwal4914
    @ankitagarwal4914 Před 2 lety +5

    Thank You Sir!, 290. Word Pattern question was asked to me in Zoom interview (San Francisco). I did same mistake of not mapping bidirectional. Thanks for clarification.

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Always there for you guys. for better experience and precisely arranged content visit on nados.pepcoding.com
      Don't forget to follow us on Instagram instagram.com/pepcoding/

  • @varuntaneja7073
    @varuntaneja7073 Před 2 lety

    what if there was a character from str1 in str2 , like there's an 'a' instead of 'z' in str2. Now, 'a' would be mapped with 'x' and 'c' would be mapped with 'a'. So its not a one to one relation.

  • @alpishjain1317
    @alpishjain1317 Před 3 lety +7

    your explanations are awesome!!! I don't understand why your videos are underrated.

    • @Pepcoding
      @Pepcoding  Před 3 lety +18

      Because motivational gyan videos are over rated

  • @deepeshagarwal2431
    @deepeshagarwal2431 Před 2 lety +1

    after watching 2-3 videos of FAANG ytubers, I land up on this. Thanks

  • @gokulnaathb2627
    @gokulnaathb2627 Před 2 lety

    Amazing explanation, Sir 🥰

  • @vipulrohilla1383
    @vipulrohilla1383 Před rokem

    Thanks Sir for explaining beautifully :)

  • @faizanahmed9304
    @faizanahmed9304 Před rokem

    Wonderful expalanation!

  • @TayiAbhignyaBCE
    @TayiAbhignyaBCE Před rokem

    Thank you for amazing expanation sir

  • @itachiuchiha-vs3qb
    @itachiuchiha-vs3qb Před 3 lety

    you are great!! I can't even watch other youtubers videos now..

  • @cr7johnChan
    @cr7johnChan Před 2 lety +2

    //Thank You Sir, your hint helped me a lot . The examples in certain platforms are just hillarious
    int n=str1.size();
    int m=str2.size();
    if(n!=m)return false;
    maptwo;
    mapumap;
    for(int i=0;i

  • @pran7119
    @pran7119 Před rokem

    @Pepcoding paper and title is isomorphic string as per leetcode, but as per your code its not isomorphic.

  • @gauravkumar-hj3pj
    @gauravkumar-hj3pj Před 2 lety +4

    this code is working fine in all scenarios except the below Test case.
    String s1 = "paper";
    String s2 = "title"
    can someone give a try.

    • @rahulranjan7567
      @rahulranjan7567 Před 7 měsíci

      Its because the 'e' in both are considered different. If we assume it to be same then this solution will fail.
      For that you need to have two hashmaps and see the one on one mapping

  • @ayushgoel9584
    @ayushgoel9584 Před 3 lety

    Sir HashMap ke bade tricky questions lagte the mujeh pehle, saare ratte wale, lekin acquire and release strategy se arram se ho gye, no need of cramping, 1 din mein 25 questions acquire and release se ho gye, great strategy

    • @Pepcoding
      @Pepcoding  Před 3 lety

      haina? Jab full 100 ho jaenge tab bhot sare array and strings ke questions bhi ho chuke honge

  • @Ankit.yt_885
    @Ankit.yt_885 Před 2 lety

    Very Nice explanation...

  • @codeblessing3149
    @codeblessing3149 Před rokem

    Good job 👍

  • @anjalidamani8255
    @anjalidamani8255 Před 3 lety +1

    Sir in your solution you have used two Hashmaps, I have solved with one HashMap (for character to character mapping ) and one HashSet( for checking the test cases when same character of string t is mapped to different characters of string s ).
    Which one of the two solutions(in terms of data structure and complexity) will be better ?

    • @Pepcoding
      @Pepcoding  Před 3 lety +2

      Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.

    • @sujan_kumar_mitra
      @sujan_kumar_mitra Před 3 lety +2

      HashSet internally uses HashMap. So TC will be same

    • @mickyman753
      @mickyman753 Před 2 lety +1

      yeah ,checking for boolean is same as contains check

  • @rahulranjan7567
    @rahulranjan7567 Před 7 měsíci

    You can use a hashset as well as a pool of chars which r already seen

  • @vishalkushwaha5373
    @vishalkushwaha5373 Před 3 lety

    sir test cases strong nhi hai portal pr
    a b b a
    dog dog dog dog
    iske lie correct de rha hai update kr do

  • @pavani7362
    @pavani7362 Před rokem

    what is tc and sc ?

  • @youngshahrukhkhan8179
    @youngshahrukhkhan8179 Před 3 lety

    Very Nice Explanation.....Keep making videos

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Thankyou beta!
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

  • @gauravsharma5768
    @gauravsharma5768 Před 3 lety +1

    Sir can you please provide the interview prep list currently being used in classroom courses at pepcoding.

    • @Pepcoding
      @Pepcoding  Před 3 lety

      beta level2 mei latest aa jaega sab.

    • @gauravsharma5768
      @gauravsharma5768 Před 3 lety

      @@Pepcoding sir wo to theek hai lekin levelup k questions leetcode p search karne m bahut dikkat hoti hai . Isliye mai kh rha tha ki leetcode based ek list de dijiye jisse questions dhoonde na pade baar baar . Aur sir aap bhi kehte ho ki acche test cases k liye leetcode p submit maar liya karo.

    • @Pepcoding
      @Pepcoding  Před 3 lety

      @@gauravsharma5768 ok beta, thoda time dedo firr. Mil jaegi list.

    • @gauravsharma5768
      @gauravsharma5768 Před 3 lety

      @@Pepcoding thank you sir , bahut jarurat hai uski

    • @Elon-musk-007
      @Elon-musk-007 Před 3 lety

      @@gauravsharma5768 telegram group join kar lo uss pe aur bhi material aata rehta hai

  • @ajjeuww
    @ajjeuww Před rokem

    can we solve this without map?

  • @sourabhparihar3226
    @sourabhparihar3226 Před rokem

    why this is not working
    unordered_maps;
    for(int i=0;i

  • @arjunrana4038
    @arjunrana4038 Před 2 lety

    In this question can't we use group shifted method?

    • @Pepcoding
      @Pepcoding  Před 2 lety

      For better insight, visit nados.pepcoding.com, post your doubts, community will help you out there.

  • @rameezwahid3626
    @rameezwahid3626 Před 3 lety

    Sir pepcoding ke videos CZcams pe Kab tak available rahenge?

  • @2amCoder
    @2amCoder Před 4 měsíci

    if (map.containsKey(chS)) {
    if (map.get(chS) != chT)
    return false;
    } else {
    if (map.containsValue(chT))
    return false;
    map.put(chS, chT);
    } sir hamlog ek map se bhi kar skate hai ki agr chS conatins karta hai to vo chT hi hoga and agar nahi karta hai to
    ye checkKar lenge ChT ka value alreafy hai ki nahi

  • @noobhike5609
    @noobhike5609 Před 3 lety

    Thank you sir 🙏🙏. Sir graph level up upload kijiye please 🙏🙏

  • @mriduljain1981
    @mriduljain1981 Před rokem

    bool isIsomorphic(string s, string t) {
    map mp;
    for(int i=0;i

  • @ayushbhardwaj3296
    @ayushbhardwaj3296 Před 3 lety

    addicted to pepcoding

  • @kuldeepgiri504
    @kuldeepgiri504 Před rokem

    s1="title" t1="paper" how is isomorphic explain?

  • @payalsagar1808
    @payalsagar1808 Před 3 lety

    Awesome

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Glad to know. If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

  • @pranjulagrawal9936
    @pranjulagrawal9936 Před rokem +1

    There is no need of two HashMaps here. It can easily be done using one HashMap

  • @SumitSingh-ui4do
    @SumitSingh-ui4do Před 2 lety

    I am trying my best to increase the subscribers of this amazing channel..❤️❤️❤️❤️🙏

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Thankyou for supporting.
      Keep learning.
      And for better experience and well organised content visit nados.pepcoding.com

  • @umangkumar2005
    @umangkumar2005 Před rokem

    I think we can solve it by using two hashmap of character vs character

  • @ManishGupta-jp3dy
    @ManishGupta-jp3dy Před 3 lety

    Sir why there is so much negativity about service based companies? Does life end in infy, accenture, tcs, etc?

    • @Pepcoding
      @Pepcoding  Před 3 lety +2

      arey nahi. sabko product based company chaie bass. as such mujhe to service based company mei project change karne ki possibility ki wajah se jyada learning dikhti hai

    • @himanshushekhar3304
      @himanshushekhar3304 Před 3 lety

      @@Pepcoding is it good to join a services based company with 2 years bond.

    • @thanos9704
      @thanos9704 Před 3 lety

      @@himanshushekhar3304 nope

  • @sukhdevsharma5611
    @sukhdevsharma5611 Před 3 lety

    kya cheez ho yr tum sir...kitna acche se batate ho..

    • @Pepcoding
      @Pepcoding  Před 3 lety +1

      I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
      If you like our efforts, we request a review
      g.page/Pepcoding/review?rc
      You can subscribe to our channel here
      czcams.com/users/Pepcodingabout?view_as=subscriber

    • @sukhdevsharma5611
      @sukhdevsharma5611 Před 3 lety

      @@Pepcoding review done sir

  • @Ncrbhaichara
    @Ncrbhaichara Před 3 lety +1

    🥇😁😁

  • @67_ujjwalsolanki78
    @67_ujjwalsolanki78 Před rokem

    best paart 5:28 to 5:31

  • @abhinavgupta3913
    @abhinavgupta3913 Před 3 lety

    paper
    title
    sir is case ka answer kya hona chaiye .......... expected answer "true" de rkha h but 'e' agar 'l' ke saath map h to 'r' 'e' ke saath map nhi hona chaiye

    • @Pepcoding
      @Pepcoding  Před 3 lety +1

      Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. Therefore, we have a premium facility available for the students in which you can get the 12 hours doubt support facility. Jisme aap agr kisi bhi question main kahin bhi faste ho to aap doubt support par reach kar skte ho aur aapko TA assign ho jayega and you can get your doubt resolved from them.

    • @abhinavgupta3913
      @abhinavgupta3913 Před 3 lety

      @@Pepcoding ok sir,thank u

    • @shang_chi4651
      @shang_chi4651 Před 3 lety

      E doosri string waala hai is baar

  • @2amCoder
    @2amCoder Před 4 měsíci

    sir appp accha gana gate hai1

  • @brijyotchawla3247
    @brijyotchawla3247 Před 2 lety +2

    One of the edge cases is still failing here, try this solution with "baa" and "cfa".

  • @divyanshbarar3840
    @divyanshbarar3840 Před 2 lety

    best

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Glad you liked it!
      Keep learning
      And for better experience, visit nados.io, where you will get well curated content and career opportunities.

  • @Elon-musk-007
    @Elon-musk-007 Před 3 lety

    Aur kitne questions hai hashmap ke ????

  • @WalterWhite-zu4xz
    @WalterWhite-zu4xz Před 5 měsíci

    class Solution {
    public boolean isIsomorphic(String s, String t) {
    if(s.length() != t.length()){
    return false;
    }
    HashMap map = new HashMap();
    for(int i = 0; i < s.length(); i++){
    char sChar = s.charAt(i);
    char tChar = t.charAt(i);
    if(map.containsKey(sChar)){
    if(map.get(sChar) != tChar){
    return false; // Mismatch found
    }
    } else {
    if(map.containsValue(tChar)){
    return false; // Mapping already exists for another character
    }
    map.put(sChar, tChar); // Establishing mapping
    }
    }
    return true;
    }
    }