Created
September 28, 2019 17:43
-
-
Save beeTechMantra/3504cef4dbdd85c89ac0c447c254b30e to your computer and use it in GitHub Desktop.
Check two String is Anagram or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class CheckAnagramString { | |
public static void main(String[] args) { | |
String str1 = "beetechmantra"; | |
String str2 = "mantrabeetech"; | |
boolean anagram = isStringAnagram(str1, str2); | |
System.out.println("Is Anagram : " + anagram); | |
} | |
public static boolean isStringAnagram(String str1, String str2) { | |
if(str1 == "" || str2 == "" || str1.length() != str2.length()) { | |
return false; | |
} | |
char []charArray1 = str1.toCharArray(); | |
char []charArray2 = str2.toCharArray(); | |
Arrays.sort(charArray1); | |
Arrays.sort(charArray2); | |
for(int i=0; i<str1.length(); i++) { | |
if(charArray1[i] != charArray2[i]) | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment