HackerRank Java - Java Anagrams Solution Explained

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

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!

harivardhan
Автор

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

bartomiejkukuczka
Автор

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

edi
Автор

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

dishdog
Автор

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

saxena
Автор

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?

dharanit
Автор

Liked your value and subscribed to your channel.

memenadekhanh
Автор

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

karthikmulgundmath
Автор

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

saikrishna
Автор

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

AttackBattalion
Автор

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

ahmadalghizi
Автор

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 = + "|" + s.toUpperCase(), "");
}
return b.isEmpty();
}

ahmadalk
Автор

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;
}

BBand
Автор

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.

BBand