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
@SatishSingh-eb5tq4 ай бұрын
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?
@lalanabiridi835811 ай бұрын
instead of bucket array we can use hashtable too right??? by removing spaces
@jritzeku9 ай бұрын
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.
@mai_aasim9 ай бұрын
Thanks for helping
@eduardoignacioroblessosa63495 ай бұрын
brilliant as always
@nageshnimmala73662 жыл бұрын
Can i get by using compareTo metheod?
@nageshnimmala73662 жыл бұрын
One more request pls suggest me a book where I can understand advanced java and data structures and algorithms easily
@nikoo282 жыл бұрын
If you check the link the video description, I do mention some resources :) Hope that helps
@nageshnimmala73662 жыл бұрын
@@nikoo28 Yaa that's ok but i searched for that but it's very advanced one
@nageshnimmala73662 жыл бұрын
Pls recommend me some good books to learn servelets, jsp, microservices
@nikoo282 жыл бұрын
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 :)
@pradeepella3 жыл бұрын
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
@mr_funtor7 ай бұрын
You looped 3 times. Shouldn't this be n^3?
@nikoo287 ай бұрын
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)