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 :(
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; } }
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 ?
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?
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.
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
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 "); } } }
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 :(
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;
}
}
Wonderful Naveen thanks alot.👍
Can you pls share link of many such tricky interview questions
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 ?
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?
Yes, we are not supposed to use Arrays.sort() method
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.
Extremly Good!😁
Thanks Naveen
Sir , Why we are doing sorting here , can't we reverse both the string and then check , whether two strings are Anagram or not?
Thanks... nice 👍🏻
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 ");
}
}
Need more such amazing imp interview questions. Sir please make a vedio on imp java interview questions
Hey he already has many go check it out
@@pratikbhowmik okay got it thanks
Hi naveen, can we compare texts from two different web application using selenium.
If yes please help
Thansk
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
Are noob, boon anagrams?
Can it contain duplicate characters?
Thanks brother
why you need to sort it ?
Hi Naveen. Your videos are awesome. Telegram link has expired. Share the new link.
t.me/joinchat/9FrG-KzGlvxjNmQ1
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 ");
}
}
}
Can you upload the Java code for all UA-cam tutorials to Github?
github.com/naveenanimation20/JavaTopics2021.git