Java Anagrams

  • + 1 comment

    I know this ain't a appropriate way for a soln but I just started solving coding proplems...please give me a tips to improve thanks!

    import java.util.Scanner;

    public class Solution {

    static boolean isAnagram(String a, String b) {
        // Complete the function
      a = a.toLowerCase();
      b = b.toLowerCase();
    
      if (a.length() != b.length()){
          return false;
      }
    
       char[] b_arr = b.toCharArray();
    
       for(int i = 0; i< a.length(); i++){
           boolean matchfound = false;
    
           for(int j = 0; j< b_arr.length;j++){
               if(a.charAt(i) == b_arr[j]){
    
                   b_arr[j] = '\0';
                   matchfound = true;
                   break;
               }   
           }
           if(!matchfound){
               return false;
           }
       }  
       return true;  
    }
    
    public static void main(String[] args) {
    
        Scanner scan = new Scanner(System.in);
        String a = scan.next();
        String b = scan.next();
        scan.close();
        boolean ret = isAnagram(a, b);
        System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
    }
    

    }