Advertisement
Guest User

Untitled

a guest
Sep 11th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class WordAnagrams {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.        String word = scanner.nextLine();
  11.        int num = Integer.parseInt(scanner.nextLine());
  12.  
  13.         for (int i = 0; i < num; i++) {
  14.             String[]anagram = scanner.nextLine().split("");
  15.             if (anagram.length == word.length()) {
  16.                 List<String> wordChars = Arrays.stream(word.split("")).collect(Collectors.toList());
  17.                 for (int j = 0; j < anagram.length; j++) {
  18.                     wordChars.remove(anagram[j]);
  19.                 }
  20.                 if (wordChars.isEmpty()) {
  21.                     System.out.println("Yes");
  22.                 } else {
  23.                     System.out.println("No");
  24.                 }
  25.             } else {
  26.                 System.out.println("No");
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement