Created
January 1, 2020 08:25
-
-
Save ssaurel/cdec47331dd1cd16291d3671b202c6bd to your computer and use it in GitHub Desktop.
sameLetters method for the Anagram Finder tutorial on the SSaurel's Blog
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
| // Method for comparing two strings and returning true if they have same letters | |
| public static boolean sameLetters(String a, String b) { | |
| if (a == null) | |
| return b == null; | |
| if (b == null) | |
| return false; | |
| char[] left = a.toCharArray(); | |
| char[] right = b.toCharArray(); | |
| // we sort caracters of each String | |
| Arrays.sort(left); | |
| Arrays.sort(right); | |
| // we compare both sorted arrays | |
| return Arrays.equals(left, right); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment