Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- public class WordAnagrams {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String word = scanner.nextLine();
- int num = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < num; i++) {
- String[] anagram = scanner.nextLine().split("");
- if (anagram.length == word.length()) {
- char[] wordChars = word.toCharArray();
- for (int j = 0; j < anagram.length; j++) {
- int index = findIndex(wordChars, anagram[j]);
- if (index != -1) {
- wordChars[index] = '\0'; // Mark the character as used
- } else {
- System.out.println("No");
- break;
- }
- }
- if (isWordEmpty(wordChars)) {
- System.out.println("Yes");
- }
- } else {
- System.out.println("No");
- }
- }
- }
- private static int findIndex(char[] arr, String target) {
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] == target.charAt(0)) {
- return i;
- }
- }
- return -1;
- }
- private static boolean isWordEmpty(char[] arr) {
- for (char c : arr) {
- if (c != '\0') {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement