HackerRank Java - Java Anagrams Solution Explained

Поделиться
HTML-код
  • Опубликовано: 20 янв 2025
  • НаукаНаука

Комментарии • 38

  • @dishdog12
    @dishdog12 5 лет назад +11

    Hey Nick, was hoping you could help me out! On line 15 and 21 of your code you wrote "current_char-'a'" and my question is what does that do and are you subtracting the letter a or is there some kind of method I am missing. I am fairly new to this so I might just be failing to understand why this would be the way to handle this problem. Thanks in advance

    • @ahmedmoussa8748
      @ahmedmoussa8748 3 года назад +5

      i know what ur asking about, characters have an integer value for example 'a' is equal to an integer value of 97 , 'z' has an integer value of 122, and so does every letter in between.
      Similar argument with number characters, if u wanna discover by yourself try this snippet
      System.out.println((int) 'a');
      replace 'a' with any character

  • @harivardhan1395
    @harivardhan1395 2 года назад

    man the way you teach is just impressive, It was of great help. I'm really impressed with your ability to make people think. Thank you!

  • @bartomiejkukuczka7011
    @bartomiejkukuczka7011 3 года назад

    You can add if statement to second loop that checks for less than zero letter count and break loop if true.

  • @dharanit3918
    @dharanit3918 4 года назад +1

    Given a string, split it into two contiguous substrings of equal length. Determine the minimum number of characters to change to make the two substrings into anagrams of one another.
    How to find the mismatched characters and store in array bro?

    • @Ouba1
      @Ouba1 3 года назад +1

      holy shit this one seems hard af

    • @B3Band
      @B3Band 2 года назад

      Give it the string abcd
      Can't be done.

  • @ahmadalk2540
    @ahmadalk2540 5 лет назад +4

    What about this small solution :)
    static boolean isAnagram(String a, String b) {

    if(a.length() != b.length())
    return false;

    for (String s : a.split("")) {
    b = b.replaceFirst(s.toLowerCase() + "|" + s.toUpperCase(), "");
    }
    return b.isEmpty();
    }

    • @nileshmishra3192
      @nileshmishra3192 4 года назад +1

      This was a great answer by you, loved it!!
      I was checking this on further. Why did you use ----> b = b.replaceFirst(s.toLowerCase() + "|" + s.toUpperCase(), "");
      I have used the below, and it worked perfectly.
      b = b.replaceFirst(s, "");

    • @jose.brother
      @jose.brother 4 года назад +1

      @@nileshmishra3192 if you write that, 2 cases fail. because the casesensitive

    • @roshan-beAprogrammer
      @roshan-beAprogrammer 2 года назад

      no such method in String class

    • @MJRisOnline
      @MJRisOnline 2 года назад

      @@jose.brother use
      a = a.toUppercase();
      b = b.toUppercase();

  • @karthikmulgundmath6566
    @karthikmulgundmath6566 4 года назад +1

    why do you do int index = current_char-'a'; ??
    i couldnt understand.

    • @MrSp3ctre
      @MrSp3ctre 4 года назад +8

      I'm a bit late but looking at an ASCII table will help. He is trying to get the index of each letter (i.e. A = 0, B = 1, C = 2) so e.g. if the current char is c, then current char - a = 2 because of the ASCII codes (c has ASCII code 99, a has ASCII code 97, 99 - 97 = 2)

    • @menoktaokan
      @menoktaokan 4 года назад

      @@MrSp3ctre thank you man.

    • @pranavkhurud6254
      @pranavkhurud6254 4 года назад

      @@MrSp3ctre thanks

    • @Daniel-on2tf
      @Daniel-on2tf 3 года назад

      @@MrSp3ctre thank you

  • @saxena3718
    @saxena3718 3 года назад

    i think the question is slightly changed now. now they give 2 strings and we need to find min no of operation to make those 2 strings anagram of each other

  • @edi4881
    @edi4881 3 года назад

    Thanks god u exist and help us understand way better the steps. Love

  • @AttackBattalion212
    @AttackBattalion212 4 года назад

    Why wouldn’t an interviewer want to see the sorting method over a hashmap or the array solution? I had this question today for an interview

    • @tekken1935
      @tekken1935 4 года назад

      In Interviews, it is always encouraged to solve problems by using your own logic rather than using predefined language functions. It is what it is, can't do anything about it

  • @B3Band
    @B3Band 2 года назад

    static boolean isAnagram(String a, String b) {
    //get rid of the capital letter issue immediately
    a = a.toLowerCase();
    b = b.toLowerCase();

    //create two arrays to store character counts
    int[] aChars = new int[26];
    int[] bChars = new int[26];

    //iterate through String a and count the letters
    for (int i = 0; i < a.length(); i++) {
    int index = a.charAt(i);
    aChars[index - 97] ++;
    //gotta subtract 97 so char 'a' == 0
    }

    //do the same for String b
    for (int i = 0; i < b.length(); i++) {
    int index = b.charAt(i);
    bChars[index - 97] ++;
    }

    //compare the two arrays
    //if they are different at any index, return false
    for (int i = 0; i < aChars.length; i++) {
    if (aChars[i] != bChars[i]) {
    return false;
    }
    }
    //If you make through the entire loop, then the counts
    //were the same for every letter. Return true.
    return true;
    }

    • @B3Band
      @B3Band 2 года назад

      This code will also consider "Hello world" and "Hell o World" to be anagrams. Because they are. The code in the video will return false when it's actually true.
      Same for examples like "polyarch" and "holy crap" - they are anagrams. My code returns true. The video's code does not.

    • @memenadekhanh3992
      @memenadekhanh3992 Год назад

      not all heroes wear cape.

  • @B3Band
    @B3Band 2 года назад

    if (a_length != b.length) return false;
    IMMEDIATELY fails.
    It will consider "Hello World" and "h e l l o w o r l d" to not be anagrams when they clearly are.
    A less silly example would be "polyarch" and "holy crap" They are anagrams, but your algorithm says they are not.

  • @memenadekhanh3992
    @memenadekhanh3992 Год назад

    Liked your value and subscribed to your channel.

  • @ahmadalghizi5477
    @ahmadalghizi5477 4 года назад

    when you are solving for the index why is 'a' in single quotes?

    • @digantasom4015
      @digantasom4015 4 года назад +1

      He's subtracting the ASCII value of 'a' from each ASCII values of 26 alphabets, including 'a'. So that index of 'a' = 0, index of b='1' and so on..

  • @saikrishna6994
    @saikrishna6994 2 года назад

    Bro,I have exam tomorrow on java could u help me...

  • @automanautomation9845
    @automanautomation9845 3 года назад

    Thank you man, really helpful video

  • @ramyabl6753
    @ramyabl6753 4 года назад

    Thank u man nice explaination....!!

  • @arkasingha6111
    @arkasingha6111 3 года назад

    Awesome bro ❤❤

  • @suheltadsare9998
    @suheltadsare9998 4 года назад

    thanx nick

  • @vinci_irl
    @vinci_irl 2 года назад

    wow the cheat codes are real

  • @shreeyabhosale1456
    @shreeyabhosale1456 3 года назад

    nice logies