Determine if two strings/phrases are valid Anagrams | Study Algorithms

Поділитися
Вставка
  • Опубліковано 23 січ 2025

КОМЕНТАРІ • 17

  • @anirudhv0062
    @anirudhv0062 9 місяців тому +2

    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-eb5tq
    @SatishSingh-eb5tq 8 місяців тому +4

    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?

  • @lalanabiridi8358
    @lalanabiridi8358 Рік тому +3

    instead of bucket array we can use hashtable too right???
    by removing spaces

    • @jritzeku
      @jritzeku Рік тому

      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.

  • @eduardoignacioroblessosa6349
    @eduardoignacioroblessosa6349 8 місяців тому

    brilliant as always

  • @feruzayusupova1672
    @feruzayusupova1672 3 місяці тому +1

    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

  • @mai_aasim
    @mai_aasim Рік тому +1

    Thanks for helping

  • @nageshnimmala7366
    @nageshnimmala7366 2 роки тому

    Can i get by using compareTo metheod?

  • @nageshnimmala7366
    @nageshnimmala7366 2 роки тому

    One more request pls suggest me a book where I can understand advanced java and data structures and algorithms easily

    • @nikoo28
      @nikoo28  2 роки тому +1

      If you check the link the video description, I do mention some resources :)
      Hope that helps

    • @nageshnimmala7366
      @nageshnimmala7366 2 роки тому

      @@nikoo28
      Yaa that's ok but i searched for that but it's very advanced one

    • @nageshnimmala7366
      @nageshnimmala7366 2 роки тому

      Pls recommend me some good books to learn servelets, jsp, microservices

    • @nikoo28
      @nikoo28  2 роки тому

      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 :)

  • @mr_funtor
    @mr_funtor 11 місяців тому

    You looped 3 times. Shouldn't this be n^3?

    • @nikoo28
      @nikoo28  10 місяців тому +4

      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)

    • @mr_funtor
      @mr_funtor 10 місяців тому +1

      @@nikoo28 Alright. Thanks

  • @pradeepella
    @pradeepella 3 роки тому

    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