Java Anagrams

Sort by

recency

|

2433 Discussions

|

  • + 0 comments
    static boolean isAnagram(String a, String b) {
        String s1=a.toLowerCase();
        String s2=b.toLowerCase();
    
        int [] countofs1= new int[26];
        int [] countofs2= new int[26];
        for(char c:s1.toCharArray()){
            countofs1[c-'a']++;
        }
    
    
        for(char c:s2.toCharArray()){
            countofs2[c-'a']++;
        }
    
    
        for(int i=0;i<26;i++){
            if(countofs1[i] != countofs2[i]){
    
                return false;
            }
        }
    
        return true;
    
    }
    
  • + 0 comments
    static boolean isAnagram(String a, String b) {
        // Complete the function
        java.util.Map<Character, Integer> stringAMap = new java.util.HashMap<>();
        java.util.Map<Character, Integer> stringBMap = new java.util.HashMap<>();
    
        if(a.length() == b.length()) {
            for(int i = 0; i < a.length(); i++){
                stringAMap.compute(a.toLowerCase().charAt(i), (key, value) -> value == null ? 1 : value + 1);    
                stringBMap.compute(b.toLowerCase().charAt(i), (key, value) -> value == null ? 1 : value + 1);    
            }
            if(stringAMap.equals(stringBMap)){
                return true;
            }
            return false;
    
        }
        return false;
    }
    
  • + 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" );
    }
    

    }

  • + 0 comments

    static boolean isAnagram(String a, String b) {

    1. //Checking two strings lengths

      if(a.length()!=b.length()) 
      {
                  return false;
      }
      

      2.//Converting the string of characters into Uppercase (we can use Lowercase too)

      a=a.toUpperCase();        
      b=b.toUpperCase();
      

    3.//converting strings into Character Array

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

    4.//Sorting two Character Arrays

        char temp;
        for(int i=0;i<=ch1.length-1;i++)
        {
            for(int j=i+1;j<=ch1.length-1;j++)
            {
                if(ch1[i]>ch1[j])
                {
                    temp=ch1[i];
                    ch1[i]=ch1[j];
                    ch1[j]=temp;
                }
                if(ch2[i]>ch2[j])
                {
                    temp=ch2[i];
                    ch2[i]=ch2[j];
                    ch2[j]=temp;
                }
            }
        }
    
    1. //Checking the two Arrays character by character

      for(int i=0;i<=ch1.length-1;i++) { if(ch1[i]!=ch2[i]) { return false; } } return true;

      }

  • + 0 comments
    import java.util.Scanner;
    
    public class Solution {
    
        static boolean isAnagram(String a, String b) {
            if(a.length() != b.length()){
                return false;
            }
            String A = a.toLowerCase();
            String B = b.toLowerCase();
            java.util.Set<String> charSet = new java.util.HashSet<>();
            for(char a1: A.toCharArray()){
                if(!charSet.contains(String.valueOf(a1))){
                    charSet.add(String.valueOf(a1));
                    int counter1 = 0;
                    int counter2 = 0;
                    for(char a2: A.toCharArray()){
                        if(a1 == a2){
                            counter1++;
                        }
                    }
                    for(char b1: B.toCharArray()){
                        if(a1 == b1){
                            counter2++;
                        }
                    }
                    if(counter1 != counter2){
                        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" );
        }
    }