Advertisement
BojidarDosev

anagram check with 2 lists

May 5th, 2025
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         String s1 = sc.nextLine();
  7.         String s2 = sc.nextLine();
  8.  
  9.         System.out.println(isIt(s1, s2));
  10.  
  11.         sc.close();
  12.     }
  13.  
  14.    public static boolean isIt(String s1, String s2){
  15.         boolean result = true;
  16.         s1 = s1.toLowerCase(); s2 = s2.toLowerCase();
  17.  
  18.        List<Character> a1 = new ArrayList<Character>();
  19.        List<Character> a2 = new ArrayList<Character>();
  20.        for (char c : s1.toCharArray()) {
  21.            a1.add(c);
  22.        }
  23.        for (char c : s2.toCharArray()) {
  24.            a2.add(c);
  25.        }
  26.        Collections.sort(a1);Collections.sort(a2);
  27.        
  28.         if(s1.length() != s2.length()){
  29.             result = false;
  30.             return result;
  31.         }
  32.         else{
  33.             for(int i = 0; i < a1.size(); i++){
  34.                 char a1char = a1.get(i);
  35.                 char a2char = a2.get(i);
  36.                 if(a1char != a2char){
  37.                     result = false;
  38.                     break;
  39.                 }
  40.             }
  41.         }
  42.         return result;
  43.    }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement