Created
March 25, 2023 04:18
-
-
Save BeboKhouja/6567c6a6a97b846deabd84e501b703c0 to your computer and use it in GitHub Desktop.
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.*; | |
| 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