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

preview_player
Показать описание
In this video, I have explained How to Check Two Strings Are Anagram Or Not using Sort and Equals in Java.

~~~Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:

Follow me on my Facebook Page:

Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:

Naveen AutomationLabs Paid Courses:
Java & Selenium:

Java & API +POSTMAN + RestAssured + HttpClient:
Рекомендации по теме
Комментарии
Автор

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 ");
}

}

karishmakhan
Автор

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;
}

}

namitgupta
Автор

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

ashwinib
Автор

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

vinitp
Автор

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 ?

swathiu
Автор

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?

souvikchatterjee
Автор

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

hemanthkumar
Автор

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

kapilsingh
Автор

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++) {

x = a1.charAt(i);

y = a2.charAt(j);
(a1.charAt(i) == a2.charAt(j)) {





}

}
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 ");
}

}

}

bihari
Автор

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

vengateshm
Автор

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

MrAnkit
Автор

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

felixrajan
Автор

Can you upload the Java code for all YouTube tutorials to Github?

DeepakLalchandaniProfile