Java Anagrams

Sort by

recency

|

2453 Discussions

|

  • + 0 comments
    public class Solution {
    
        static boolean isAnagram(String a, String b) {
            if (a.length() != b.length()) return false;
            if (sortAsc(a).compareTo(sortAsc(b)) != 0) return false;
            return true;    
        }
        
        //  Bubble Sort Algorithm
        public static String sortAsc(String value){        
            char[] c = value.toLowerCase().toCharArray();
            for (int i=0;i<c.length-1;i++){
                for (int j=0;j<c.length-1-i;j++){
                    if(c[j]> c[j+1]){
                        char temp = c[j];
                        c[j] = c[j+1];
                        c[j+1] = temp;
                    }
                }
            }
            return new String(c);
        }
    
  • + 0 comments
    static boolean isAnagram(String a, String b) {
            // Complete the function
            int[]map = new int[26];
            for(char x: a.toLowerCase().toCharArray()) map[(int)x - (int)'a']++;
            for(char x: b.toLowerCase().toCharArray()) map[(int)x - (int)'a']--;
            for(int x: map) if(x != 0) return false;
            return true;
    }
    
  • + 1 comment

    My solution. I don't use class Arrays from java.util.Arrays

    static boolean isAnagram(String a, String b) {
            // Complete the function
            char[] aa = a.toLowerCase().toCharArray();
            char[] bb = b.toLowerCase().toCharArray();                      
            
            if (aa.length != bb.length)
                return false;
            
            for (int i = 0; i < aa.length; i++){
                char s1 = aa[i];
                char s2 = bb[i];
                int fs1 = 0;
                int fs2 = 0;
                
                for (int j = 0; j < aa.length; j++){
                    if (aa[j] == s1)
                        fs1++;
                    if (bb[j] == s1)
                        fs2++;
                }
                if (fs1 != fs2)
                    return false;
            }
            
            return 
                true;
        }
    
  • + 0 comments

    My solution :)

    static boolean isCharConsidered(char[] chs, char ch) {
          for(int i = 0; i < chs.length; i++) {
            if(chs[i] == ch)
              return true;
          }
          return false;
        }
        
        static int getCntOfChar(byte[] chs, char ch) {
          int res = 0;
          for(int i = 0; i < chs.length; i++) {
            if(chs[i] == ch)
              res++;
          }
          return res;
        }
        
        static boolean isAnagram(String a, String b) {
    
          if(a.length() != b.length())
            return false;
          
          // Complete the function
          byte[] acs = a.toLowerCase().getBytes();
          byte[] bcs = b.toLowerCase().getBytes();
    
          int nextChar = 0;
          char[] chars = new char[a.length()];
    
          for(int i = 0; i < a.length(); i++) {
            if(!isCharConsidered(chars, a.charAt(i))){
              chars[nextChar++] = a.charAt(i);
              if (getCntOfChar(acs, a.charAt(i)) != getCntOfChar(bcs, a.charAt(i)))
                return false;
            }
          }
    
          return true;
        }
    
  • + 1 comment

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

    public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String a=sc.next();
        String b=sc.next();
        boolean res=isAnagram(a,b);
        if(res){
            System.out.println("Anagrams");
        }
        else{
            System.out.println("Not Anagrams");
        }
     }
     static boolean isAnagram(String a,String b){
        a=a.toLowerCase();
        b=b.toLowerCase();
        if(a.length()!=b.length()){
            return false;
        }
        char ch1[]=a.toCharArray();
        char ch2[]=b.toCharArray();
        Arrays.sort(ch1);
        Arrays.sort(ch2);
        return Arrays.equals(ch1,ch2);
    

    } }