MiniMi2022

Word anagrams

Feb 15th, 2022 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class WordAnagrams {
  4.     public static void main(String[] args) {
  5.         Scanner myScan=new Scanner(System.in);
  6.         String word=myScan.nextLine();
  7.         int n=Integer.parseInt(myScan.nextLine());
  8.         for (int i = 0; i < n; i++) {
  9.             String wordCurrent=myScan.nextLine();
  10.             boolean isAnagram=true;
  11.             for (int j = 0; j < word.length(); j++) {
  12.                 String ch=String.valueOf(word.charAt(j));
  13.                 if (!wordCurrent.contains(ch)){
  14.                     isAnagram=false;
  15.                     break;
  16.                 }else{
  17.                     wordCurrent=wordCurrent.replaceFirst(ch,"");
  18.                 }
  19.             }
  20.             System.out.println(isAnagram?"Yes":"No");
  21.         }
  22.     }
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment