We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Java
  3. Strings
  4. Java Anagrams
  5. Discussions

Java Anagrams

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 2001 Discussions, By:

recency

Please Login in order to post a comment

  • i_m_aryan_20
    7 hours ago+ 0 comments

    Simple Easy Java 7 Solution

    **If you find it helpful the please upvote it **

    static boolean isAnagram(String s1, String s2) {
            // Complete the function
            String a = s1.toLowerCase();
            String b = s2.toLowerCase();
            
            char []a1 = a.toCharArray();
            char []b1 = b.toCharArray();
            
            java.util.Arrays.sort(a1);
            java.util.Arrays.sort(b1);
            
            if(a1.length!= b1.length) return false;
            int n = a1.length; // we can write int n = b1.length as well
            for(int i = 0;i < n; i++){
                if(a1[i] != b1[i])
                    return false;
            }
            return true;
        }
    
    0|
    Permalink
  • siddhusadadekar1
    2 days ago+ 0 comments

    static boolean isAnagram(String a, String b) { String strA = a.toLowerCase(); String strB = b.toLowerCase();

    char[] charA = strA.toCharArray();
    char[] charB = strB.toCharArray();
    
    java.util.Arrays.sort(charA);
    java.util.Arrays.sort(charB);
    
    boolean result = java.util.Arrays.equals(charA, charB);
    return result;
    }
    
    0|
    Permalink
  • concealedgloomy
    2 days ago+ 1 comment
    public static boolean isAnagram(String a, String b) {
    
        a = a.toLowerCase();
        b = b.toLowerCase();
    
        char[] ac = a.toCharArray();
        char[] bc = b.toCharArray();
    
        java.util.Arrays.sort(ac);
        java.util.Arrays.sort(bc);
    
        return String.valueOf(ac).equals(String.valueOf(bc));
    
    }
    
    0|
    Permalink
  • riccardotapparo1
    2 days ago+ 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);
            return java.util.Arrays.equals(a1, b1);
        }
    
    0|
    Permalink
  • akabynga
    3 days ago+ 0 comments

    I think it's the one of the most efficient way to solve this problem)

    static boolean isAnagram(String a, String b) {
    		a = a.toUpperCase();
    		b = b.toUpperCase();
    		if(a.length() != b.length()) {
    				return false;
    		}
    		int[] array = new int[256];
    
    		for(int i = 0; i < a.length();i++) {
    				array[a.charAt(i)]++;
    				array[b.charAt(i)]--;
    		}
    
    		for(int i : array){
    				if(i != 0){
    						return false;
    				}
    		}
    
    		return true;        
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy