In the Optimized code, I don't think we need to check for spaces. Since anagrams are supposed to be single words, if there are spaces the string, it will become a phrase instead of a word. Also if we check in the beginning if the strings are of equal length, there is no need for 2 for loops of bucket arrays. The code can be simplified to : class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) return false; int counts = new int[26]; for (int i=0; i
I took this approach: -remove all empty spaces first -early exit cond; if length dont equal -In first loop evaluated first string populated hash map with occurrence of each char. -In second loop, evaluated second string and decremented the count from map. If the count was at 1, i removed it from map. -Ultimately, at the end if the map is empty (length of 0), we found anagram.
Thank you Nikhil for the video. I finally understood. But I still have a question. What is this means : counts[s.charAt(i)-'a']++; I don't understand (arr[]++) array increment is not allowed in Java
Have you tried the headfirst series? They are really easy to follow, have a lot of visuals and you will make projects too as you read along. Highly recommended :)
In the Optimized code, I don't think we need to check for spaces. Since anagrams are supposed to be single words, if there are spaces the string, it will become a phrase instead of a word. Also if we check in the beginning if the strings are of equal length, there is no need for 2 for loops of bucket arrays. The code can be simplified to :
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
int counts = new int[26];
for (int i=0; i
Why didn't you used HashMap to fill the values first with key and counter and then to decrement. Why to go for A-Z map?
instead of bucket array we can use hashtable too right???
by removing spaces
I took this approach:
-remove all empty spaces first
-early exit cond; if length dont equal
-In first loop evaluated first string populated hash map with occurrence of each char.
-In second loop, evaluated second string and decremented the count from map. If the count was at 1, i removed it from
map.
-Ultimately, at the end if the map is empty (length of 0), we found anagram.
brilliant as always
Thank you Nikhil for the video. I finally understood. But I still have a question. What is this means : counts[s.charAt(i)-'a']++; I don't understand (arr[]++) array increment is not allowed in Java
Thanks for helping
Can i get by using compareTo metheod?
One more request pls suggest me a book where I can understand advanced java and data structures and algorithms easily
If you check the link the video description, I do mention some resources :)
Hope that helps
@@nikoo28
Yaa that's ok but i searched for that but it's very advanced one
Pls recommend me some good books to learn servelets, jsp, microservices
Have you tried the headfirst series? They are really easy to follow, have a lot of visuals and you will make projects too as you read along. Highly recommended :)
You looped 3 times. Shouldn't this be n^3?
3 loops do not mean O(n^3)...3 nested loops will cause the time complexity to be n^3.
3 separate loops mean O(3 * n) = O(n)
@@nikoo28 Alright. Thanks
package javaprac;
public class JavaPrac {
public boolean anagram(String str1,String str2){
str1=str1.toLowerCase();
str2=str2.toLowerCase();
str1=str1.replace(" ", "");
str2=str2.replace(" ", "");
int[] arr=new int[26];
for(int i=0;i