Java Anagrams

Sort by

recency

|

2446 Discussions

|

  • + 0 comments
    if(a.length() != b.length()) {
                return false;
            }
            char[ ] a1 = a.toLowerCase().toCharArray();
            char[ ] b1 = b.toLowerCase().toCharArray();
            java.util.Arrays.sort(a1);
            java.util.Arrays.sort(b1);
    
            return java.util.Arrays.equals(a1, b1);
    
  • + 0 comments

    i was done it , without using Arrays.sort() ---> but Time Compelxity is O(n^2) in worst case scenario :(

    char ch1[] = a.toLowerCase().toCharArray(); char ch2[] = b.toLowerCase().toCharArray();

        for(int c = 0; c < ch1.length-1; c++){
            for(int cc = 0; cc < ch1.length-c-1; cc++){
                if(ch1[cc] > ch1[cc+1]){
                    //swap
                    char temp = ch1[cc];
                    ch1[cc] = ch1[cc+1];
                    ch1[cc+1] = temp;
    
                }
            }
        }
    
        String ch1_str = new String(ch1);
    
        // 
        for(int c = 0; c < ch2.length-1; c++){
            for(int cc = 0; cc < ch2.length-c-1; cc++){
                if(ch2[cc] > ch2[cc+1]){
                    //swap
                    char temp = ch2[cc];
                    ch2[cc] = ch2[cc+1];
                    ch2[cc+1] = temp;
    
                }
            }
        }
    
        String ch2_str = new String(ch2);
    
    
       if(ch1_str.equals(ch2_str)){
        return true;
       }
    
       return false;
    
    }
    
  • + 0 comments

    static boolean isAnagram(String a, String b){

        if(a.length()!=b.length()){
            return false;
        }
        char[] a1=a.toLowerCase().toCharArray();
        char[] b1=b.toLowerCase().toCharArray();
        java.util.Arrays.sort(a1);
        java.util.Arrays.sort(b1);
    
        if(java.util.Arrays.equals(a1,b1)){
            return true;
        }
        return false;
    }
    
  • + 0 comments

    we can not use any library to since import is already disbaled. we can use the loop to find the frequency of every char and we can check if the values of count of each char is equal in every iteration and if length of string is equal then frequency will never gets equal.

    import java.util.Scanner;
    
    public class Solution {
        static boolean isAnagram(String a, String b) {
            // Complete the function
            if(a.length()!=b.length())
            return false;
            a=a.toLowerCase();
            b=b.toLowerCase();
            for(char c:a.toCharArray()){
                int counta=0;
            int countb=0;
                for(int j=0;j<a.length();j++){
                if(c==a.charAt(j))
                 counta++;
                 if(c==b.charAt(j))
                 countb++;
                 
            }
            if(counta!=countb)
            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" );
        }
    }
    
  • + 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);
    }
    

    }