Advertisement
Guest User

Untitled

a guest
Aug 31st, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. package sam;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class wordAnagram {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10.  
  11. String word1 = scanner.nextLine();
  12.  
  13. int n = scanner.nextInt();
  14.  
  15. for (int i = 1; i <= n; i++) {
  16. String word2 = scanner.nextLine();
  17. if(isAnagram(word1,word2)) {
  18. System.out.println("Yes");
  19. }else {
  20. System.out.println("No");
  21. }
  22. }
  23.  
  24.  
  25. }
  26.  
  27.  
  28. private static boolean isAnagram(String word1, String word2) {
  29. word1 = word1.replaceAll("\\s", "").toLowerCase();
  30. word2 = word2.replaceAll("\\s", "").toLowerCase();
  31.  
  32. char[] word1Arr = word1.toCharArray();
  33. char[] word2Arr = word2.toCharArray();
  34.  
  35. Arrays.sort(word1Arr);
  36. Arrays.sort(word2Arr);
  37.  
  38. return (Arrays.equals(word1Arr, word2Arr));
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement