Check Two Strings are Anagram Or Not using Sort and Equals in Java

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

КОМЕНТАРІ •

  • @ashwinib5017
    @ashwinib5017 3 роки тому +2

    Thanks Naveen....got this question in Oracle interview today. I had given the above solution only, was asked if one of the string is Null how will he handle write the code, and interviewer asked me for other ways also like Hashmap couldn't answer that :(

  • @namitgupta352
    @namitgupta352 3 роки тому +5

    Thanks for a different approach in this video. I always learn something new from u.
    Another approach using array mapping:
    public class Solution {
    public static void main(String[] args) {
    System.out.println(check_anagrams("listen", "SILEN T"));
    }
    public static boolean check_anagrams(String s1, String s2) {
    s1 = s1.replaceAll("\\s", "");
    s2 = s2.replaceAll("\\s", "");
    if (s1.length() != s2.length()) return false;
    s1 = s1.toLowerCase();
    s2 = s2.toLowerCase();
    int[] abcd = new int[26];
    for (int i = 0; i < s1.length(); i++) {
    abcd[(int) s1.charAt(i) - 97]++;
    }
    for (int i = 0; i < s2.length(); i++) {
    int c = (int) s2.charAt(i) - 97;
    if (abcd[c] > 0) {
    abcd[c]--;
    } else {
    return false;
    }
    }
    return true;
    }
    }

  • @vinitp4559
    @vinitp4559 3 роки тому +1

    Wonderful Naveen thanks alot.👍
    Can you pls share link of many such tricky interview questions

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

    Hi Naveen , This solution is really helped me to understand . But how to identify the aranagrams in the given list of words example { car ,arc, , ate,eat , cat etc . Could you please provide the solutions for this ?

  • @souvikchatterjee7400
    @souvikchatterjee7400 3 роки тому +1

    Hi Naveen, I have an upcoming interview for a project in my company. We had a call with the project manager and he mentioned that if you use Arrays.sort() or any other internal sorting method, they reject the solution. In such cases like the one you mentioned, it gives optimised solution? What are your views on this?

    • @DeepakLalchandaniProfile
      @DeepakLalchandaniProfile 3 роки тому +1

      Yes, we are not supposed to use Arrays.sort() method

    • @alinabehera2152
      @alinabehera2152 3 роки тому +2

      Yes, In most of the interviews they do not accept the Sort() method if the question is related to only sorting an array but in this type of questions it should be Ok to answer this.

  • @A98-t3o
    @A98-t3o 2 роки тому

    Extremly Good!😁

  • @sandeepkokane9549
    @sandeepkokane9549 3 роки тому +1

    Thanks Naveen

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

    Sir , Why we are doing sorting here , can't we reverse both the string and then check , whether two strings are Anagram or not?

  • @onlyforautomation6450
    @onlyforautomation6450 3 роки тому +1

    Thanks... nice 👍🏻

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

    public static void main(String[] args) {
    String s1 ="Listen";
    String s2 = "silent";
    s1 = s1.toLowerCase();
    s2= s2.toLowerCase();
    if(s1.length()== s2.length()) {
    char[] charArray1 = s1.toCharArray();
    char[] charArray2 = s2.toCharArray();
    Arrays.sort(charArray1);
    Arrays.sort(charArray2);
    boolean result = Arrays.equals(charArray1, charArray2);
    if(result==true) {
    System.out.println(s1 + " and " + s2 +" are anagram");
    }
    else {
    System.out.println(s1 + " and " + s2 +" are not anagram");
    }
    }
    else {
    System.out.println(s1 + " and " + s2 +" are not anagram, length are not same ");
    }
    }

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

    Need more such amazing imp interview questions. Sir please make a vedio on imp java interview questions

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

    Hi naveen, can we compare texts from two different web application using selenium.
    If yes please help
    Thansk

    • @Chethan-l5s
      @Chethan-l5s 6 місяців тому

      You mean 2 different websites? Definitely yes. Switch to the website you want to get the text and comeback to the parent website [use window handles] and compare now

  • @vengateshm2122
    @vengateshm2122 3 роки тому +1

    Are noob, boon anagrams?
    Can it contain duplicate characters?

  • @pandudamera7211
    @pandudamera7211 3 роки тому +1

    Thanks brother

  • @nadiff9723
    @nadiff9723 Місяць тому +1

    why you need to sort it ?

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

    Hi Naveen. Your videos are awesome. Telegram link has expired. Share the new link.

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

    public class Anagram {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String a1 = "triangle";
    String a2 = "intergal";
    int count = 0;
    if (a1.length() == a2.length()) {
    for (int i = 0; i < a1.length(); i++) {
    for (int j = 0; j < a2.length(); j++) {
    char x = a1.charAt(i);

    char y = a2.charAt(j);
    if (a1.charAt(i) == a2.charAt(j)) {
    count++;
    break;
    }
    }
    }
    if (count == a1.length()) {
    System.out.println("Yes anagram");
    } else {
    System.out.println("Not anagram only same length and count is " + count);
    }
    } else {
    System.out.println("Length not matching to be anagram ");
    }
    }
    }

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

    Can you upload the Java code for all UA-cam tutorials to Github?