Advertisement
Guest User

Mock Exam 2, Таск 3

a guest
Mar 2nd, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import java.util.Scanner; public class Exam_2_T_2 { public static void main(String[] args) {
  2. Scanner scanner = new Scanner(System.in);
  3.  
  4. String keyWord = scanner.nextLine();
  5. int countWords = Integer.parseInt(scanner.nextLine());
  6. String[] words = new String[countWords];
  7. int j = 0;
  8.  
  9. for (int i = 0; i < countWords; i++) {
  10. words[i] = scanner.nextLine();
  11. }
  12. for (int i = 0; i < countWords; i++) {
  13. int length = words[i].length();
  14. int length2 = keyWord.length();
  15. String wordForChek = keyWord;
  16. int counter = 0;
  17. if (words[i].length() != keyWord.length()) words[i] = "No";
  18. else while ( length > 0 ) {
  19. if (wordForChek.charAt(j) == words[i].charAt(0)) {
  20. counter++;
  21. words[i] = words[i].substring(1);
  22. wordForChek = keyWord.substring(0, j) + keyWord.substring(j+1, length2);
  23. length = words[i].length();
  24. length2--;
  25. j = 0;
  26. if (counter == keyWord.length()) {
  27. wordForChek = keyWord; break; }
  28. continue;
  29. }
  30. j++; if ( j == length ) { wordForChek = keyWord; break; }
  31. }
  32. if (counter == keyWord.length()) words[i] = "Yes";
  33. else words[i] = "No";
  34. }
  35. for (String answer : words ) System.out.println(answer);
  36. }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement