Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created January 1, 2020 08:25
Show Gist options
  • Select an option

  • Save ssaurel/cdec47331dd1cd16291d3671b202c6bd to your computer and use it in GitHub Desktop.

Select an option

Save ssaurel/cdec47331dd1cd16291d3671b202c6bd to your computer and use it in GitHub Desktop.
sameLetters method for the Anagram Finder tutorial on the SSaurel's Blog
// 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