CC-8 : Check if input Strings are anagrams.
Description:
Two Strings are said to be anagrams, if all letters in the first String are found same number of times in the second String and second String does not have any characters that are not present in the first String. Given input Strings str1 and str2, return true if the same are anagrams, else return false.
Test cases and expected outputs:
| Input Parameters | Expected outputs |
|---|---|
| str1="Listen"; str2="Silent"; |
The input string are anagrams. |
| str1="Secure"; str2="Rescue"; |
The input string are anagrams. |
| str1="Note"; str2="tonn"; |
The input string are *not* anagrams. |
Pseudocode:
| The java method should accept following input parameters: str1 (String), str2 (String). |
| Call String.trim() method on str1 and str2 to remove leading and trailing spaces. |
| Call String.toLowerCase() method on str1 and str2 to make the characters lowercase. |
| Initialize char array chars1= str1.toCharArray(). |
| Initialize char array chars2= str2.toCharArray(). |
| Initialize boolean variable chrFound to false. We will use this to track if each char from chars1 is found in chars2. |
| Iterate through chars1 using for loop using idx1 as loop variable, starting from index 0 and incrementing idx1 till we reach chars1.length-1:
Set chrFound to false.
Iterate through chars2 using for loop using idx2 as loop variable, starting from index 0 and incrementing idx2 till we reach chars2.length-1:
If chars1[idx1]==chars2[idx2]:
set chrFound to true.
Overwrite chars2[idx2] to a blank char. This helps to avoid problems caused by matching duplicate characters that may exist in chars1 and chars2.
Break current iteration as we have already found a matching character, so no need to check the rest of the characters in chars2.
When above inner loop completes and chrFound is still false, it means the current character of chars1 does not exist in chars2, so str1 and str2 are not anagrams, return false from the program.
|
| If above outer loop is completed without returning due to mismatch, it means that str1 and str2 are anagrams. Return true. |
The above algorithm can also be completed using Java String APIs and StringBuilder APIs:
| Call String.trim() method on str1 and str2 to remove leading and trailing spaces. |
| Call String.toLowerCase() method on str1 and str2 to make the characters lowercase. |
| Initialize StringBuilder variable sb2 using String str2. |
| Initialize integer variable foundIdx to 0. |
| Iterate through str1 using for loop using idx as loop variable, starting from index 0 and incrementing idx till we reach str1.length()-1:
Using String.indexOf() method, search for str1’s character at current idx in sb2.
If str1’s current character at idx is not found in sb2, str1 and str2 are not anagrams, so return false from the program.
If str1’s current character at idx is found in sb2, delete the character that was found in sb2 using StringBuilder.deleteCharAt() method. This helps to avoid problems caused by matching duplicate characters that may exist in str1 and str2.
|
| If above loop is completed without returning due to mismatch, it means that str1 and str2 are anagrams. Return true. |
Code:
public boolean stringCheckAnagram(String str1, String str2) {
str1=str1.trim().toLowerCase();
str2=str2.trim().toLowerCase();
char[] chars1=str1.toCharArray();
char[] chars2=str2.toCharArray();
boolean chrFound;
for (int idx1=0; idx1< chars1.length;idx1++) {
chrFound=false;
for (int idx2=0; idx2< chars2.length;idx2++) {
if (chars1[idx1]==chars2[idx2]) {
chrFound=true;
chars2[idx2] = '\u0000';
break;
}
}
if (chrFound==false) {
return false;
}
}
return true;
}
public boolean stringCheckAnagramWithAPI(String str1, String str2) {
str1=str1.trim().toLowerCase();
str2=str2.trim().toLowerCase();
StringBuilder sb2=new StringBuilder(str2);
int foundIdx=0;
for (int idx=0; idx< str1.length();idx++) {
foundIdx= sb2.indexOf(str1.substring(idx,idx+1));
if (foundIdx == -1) {
return false;
}else {
sb2.deleteCharAt(foundIdx);
}
}
return true;
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |