HackerRank Strings : Making Anagrams Explained - Java

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

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

  • @megan.paffrath
    @megan.paffrath 3 года назад +4

    Great content! It is also possible to use one array :D
    // Complete the makingAnagrams function below.
    static int makingAnagrams(String s1, String s2) {
    int values[] = new int[26];
    for (int i = 0; i < s1.length(); i++) {
    values[s1.charAt(i) - 'a']++;
    }
    for (int i = 0; i < s2.length(); i++) {
    values[s2.charAt(i) -'a']--;
    }
    int removeChars = 0;
    for (int i = 0; i < values.length; i++) {
    removeChars += Math.abs(values[i]);
    }
    return removeChars;
    }

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

      nicely figured out one more solution! thanks!

  • @MegaBezalel
    @MegaBezalel 4 года назад +4

    //shorter way to increment array vals//
    for (int i = 0; i < str.length(); i++) {
    int position = str.charAt(i) -'a';
    strArray[position]++;
    }

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

    Thank you for explaining! I saw a similar solution in the Discussion board, but didn't understand the subtracting 'a' part. This clears it up.

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

    Can you share the hashmap code you started talking about in the beginning?

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

    Thanks for the advise about using alphabet letters array and for the explanation

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

    a = input()
    b = input()
    hashone = [0 for x in range(26)]
    for i in range (0,len(a)):
    hashone [ord(a[i])-97] = hashone [ord(a[i])-97] + 1
    for i in range (0,len(b)):
    hashone [ord(b[i])-97] = hashone [ord(b[i])-97] - 1
    count = 0
    for i in range (26):
    if hashone[i] < 0 or hashone[i] > 0:
    count = count + abs(hashone[i])
    print (count)

  • @Holeecrap
    @Holeecrap 5 лет назад +2

    nice explanation as well as programming skills..subbed

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

    bro you are just awesome... totally impressed....

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

    please tell me why we have to use absolute function while subtracting

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

    But why are we not considering uppercase letters...please explain.

  • @ayoolao.5865
    @ayoolao.5865 4 года назад

    Hey buddy, I understand the hashmap techniques in solving string related problems. However, I am finding it difficult to understand the 26 character technique. Could you or anyone point me to an article or video that would explain the concept in-depth and much better? thanks

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

    This was my solution using StringBuilder and a concatenated for:
    static int makeAnagram(String a, String b) {
    int ts=a.length() + b.length();
    StringBuilder nb= new StringBuilder(b);
    for(int i=0; i

  • @sangeethap.h.4926
    @sangeethap.h.4926 4 года назад

    Using single count Array
    static int makingAnagrams(String s1, String s2) {
    int [] count = new int[26];
    int res =0;
    for(char ch: s1.toCharArray()){
    count[ch-'a']++;
    }
    for(char ch: s2.toCharArray()){
    count[ch-'a']--;
    }
    for(int num: count){
    if(num !=0){
    res+=Math.abs(num);
    }
    }
    return res;
    }

  • @চলচ্চিত্রকথা

    Easier solution :
    static int makeAnagram(String a, String b) {
    int i =0;
    while (i

  • @kusumam4062
    @kusumam4062 4 года назад +3

    Im not getting & even your way of speaking is too fast

  • @EduardoMartinez-dm5pp
    @EduardoMartinez-dm5pp 2 года назад

    I came across witha solution but it took me 30 minutes... and this just took you less than 5 minutes to crack it... holy wakamoly!

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

    You are Walter White of computer science.

  • @C0DERedEdits
    @C0DERedEdits Год назад +1

    Holy shit, that's very over engineered.
    You just have to remove what exists on both Strings, whatever is left is what should be deleted, so return that...
    public static int makeAnagram(String a, String b) {
    String a2 = a;
    String b2 = b;
    for(char c : a.toCharArray()){
    b2 = b2.replaceFirst(String.valueOf(c), "");
    }
    for(char c : b.toCharArray()){
    a2 = a2.replaceFirst(String.valueOf(c), "");
    }
    return a2.length()+b2.length();
    }

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

    This could be really straight forward in JS -
    let string1Array = string1.split('');
    let string2Array = string2.split('');
    if (JSON.stringify(string1Array.sort()) === JSON.stringify(string2Array.sort())) {
    return true
    } else {
    return false
    }

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

      but if it's false, we need to know how many deletions are necessary so that it becomes an anagram. This doesn't really solve anything other than tell us whether or not it's an anagram.