Skip to content

Instantly share code, notes, and snippets.

@BeboKhouja
Created March 25, 2023 04:18
Show Gist options
  • Select an option

  • Save BeboKhouja/6567c6a6a97b846deabd84e501b703c0 to your computer and use it in GitHub Desktop.

Select an option

Save BeboKhouja/6567c6a6a97b846deabd84e501b703c0 to your computer and use it in GitHub Desktop.
import java.util.*;
public class HasSameLetters {
public static boolean madeOutOfSameLetters(String a, String b) {
if (a == null) {
return b == null;
} else if (b == null) {
return false;
}
char[] left = a.toCharArray();
char[] right = b.toCharArray();
Arrays.sort(left);
Arrays.sort(right);
return Arrays.equals(left, right);
}
public static void main(String[] args) {
Scanner test = new Scanner(System.in);
System.out.println("Enter the first word to match:");
String firstLetter = test.nextLine();
System.out.println("Enter the next word to match:");
String secondLetter = test.nextLine();
// Example: (First letter: life, Second letter: file): true
// (First letter: life, Second letter: win): false
if (madeOutOfSameLetters(firstLetter, secondLetter)) {
System.out.println("Both contain the same letters!");
} else {
System.out.println("Both do not contain the same letters!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment