Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sam;
- import java.util.Arrays;
- import java.util.Scanner;
- public class wordAnagram {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String word1 = scanner.nextLine();
- int n = scanner.nextInt();
- for (int i = 1; i <= n; i++) {
- String word2 = scanner.nextLine();
- if(isAnagram(word1,word2)) {
- System.out.println("Yes");
- }else {
- System.out.println("No");
- }
- }
- }
- private static boolean isAnagram(String word1, String word2) {
- word1 = word1.replaceAll("\\s", "").toLowerCase();
- word2 = word2.replaceAll("\\s", "").toLowerCase();
- char[] word1Arr = word1.toCharArray();
- char[] word2Arr = word2.toCharArray();
- Arrays.sort(word1Arr);
- Arrays.sort(word2Arr);
- return (Arrays.equals(word1Arr, word2Arr));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement