Java Anagrams

  • + 0 comments

    Easy solution using inBuild Arrays methods

    import java.io.; import java.util.; import java.util.Arrays;

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String a = scan.next();
        String b = scan.next();
        scan.close();
    
         if(check(a,b)){
            System.out.println("Anagrams");
        }
        else{
            System.out.println("Not Anagrams");
        }
    
    }
    public static boolean check(String s1,String s2){
        if(s1.length() != s2.length()){
            return false;
        }
        char a1[] = s1.toLowerCase().toCharArray();
        char a2[] = s2.toLowerCase().toCharArray();
        Arrays.sort(a1);
        Arrays.sort(a2);
        return Arrays.equals(a1, a2);
    }
    

    }