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
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
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?
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, "");
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)
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
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
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; }
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.
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.
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
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
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!
You can add if statement to second loop that checks for less than zero letter count and break loop if true.
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?
holy shit this one seems hard af
Give it the string abcd
Can't be done.
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();
}
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, "");
@@nileshmishra3192 if you write that, 2 cases fail. because the casesensitive
no such method in String class
@@jose.brother use
a = a.toUppercase();
b = b.toUppercase();
why do you do int index = current_char-'a'; ??
i couldnt understand.
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)
@@MrSp3ctre thank you man.
@@MrSp3ctre thanks
@@MrSp3ctre thank you
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
Thanks god u exist and help us understand way better the steps. Love
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
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
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;
}
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.
not all heroes wear cape.
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.
Liked your value and subscribed to your channel.
when you are solving for the index why is 'a' in single quotes?
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..
Bro,I have exam tomorrow on java could u help me...
Thank you man, really helpful video
Thank u man nice explaination....!!
Awesome bro ❤❤
thanx nick
wow the cheat codes are real
nice logies