Java Anagrams

Sort by

recency

|

2501 Discussions

|

  • + 0 comments

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

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=new Scanner(System.in);
        String s1=sc.nextLine();
        String s2=sc.nextLine();
    
        String s1low=s1.toLowerCase();
        String s2low=s2.toLowerCase();
    
        if(s1low.length()!=s2low.length())
        {
            System.out.println("Not Anagrams");
            return;
        }
        char s1arr[]=s1low.toCharArray();
        char s2arr[]=s2low.toCharArray();
    
        Arrays.sort(s1arr);
        Arrays.sort(s2arr);
    
        if(Arrays.equals(s1arr,s2arr))
        {
            System.out.println("Anagrams");
        }
        else{
            System.out.println("Not Anagrams");
        }
    }
    

    }

  • + 0 comments
    import java.util.Scanner;
    
    public class Solution {
        static boolean isAnagram(String a, String b) {
            a = a.toLowerCase();
            b = b.toLowerCase();
            
            int[] asciiA = new int[26];
            int[] asciiB = new int[26];
            
            if (a.length() != b.length()) {
                return false;
            }
            
            for (int i = 0; i < a.length(); i++) {
                asciiA[a.charAt(i)-97]++;
                asciiB[b.charAt(i)-97]++;
            }
            
            for (int i = 0; i < asciiA.length; i++) {
                if (asciiA[i] != asciiB[i]) {
                    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
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner sc = new Scanner(System.in);
            String A = sc.next().toLowerCase();
            String B = sc.next().toLowerCase();
            
            if(A.length() != B.length())
            {
                System.out.println("Not Anagrams");
                return;
            }
                
            HashMap<Character, Integer> map1 = new HashMap<>();
            HashMap<Character, Integer> map2 = new HashMap<>();
            
            //For A
            for (char ch : A.toCharArray()) 
            {
                if(map1.containsKey(ch))
                    map1.put(ch, Integer.valueOf(map1.get(ch) + 1));
                
                else
                    map1.put(ch, Integer.valueOf(1));
            }
            
            //For B
            for (char ch : B.toCharArray()) 
            {
                if(map2.containsKey(ch))
                    map2.put(ch, Integer.valueOf(map2.get(ch) + 1));
                
                else
                    map2.put(ch, Integer.valueOf(1));
            }
            
            //Finally comparing
            if(map1.equals(map2))
                System.out.println("Anagrams");
            else
                System.out.println("Not Anagrams");
        }
    # }
    
  • + 1 comment
    static boolean isAnagram(String a, String b) {
            // Complete the function
            if(a.length() != b.length()){
                return false;
            }
            a = a.toLowerCase();
            b = b.toLowerCase();
            int[] charCount = new int[256];
            for(char c : a.toCharArray()){
                charCount[c]++;
            }
            for(char c : b.toCharArray()){
                charCount[c]--;
            }
            
            for(int i : charCount){
                if(i != 0){
                    return false;
                }
            }
            
            return true;
            
            
        }
    
  • + 0 comments
    static boolean isAnagram(String a, String b) {
            // Complete the function
            a=a.toLowerCase();
            b=b.toLowerCase();
      int count=0;
      int count2=0;
    
      if(a.length()!=b.length())
      {
          return false;
      }
    
      for(int i=0; i<a.length(); i++)
      {
          char ch=a.charAt(i);
          for(int j=0; j<a.length(); j++)
          {
              if(a.charAt(j)==ch)
              count++;
              if(b.charAt(j)==ch)
              count2++;
          }
          if(count!=count2)
          {
          return false; 
          }
      }
      return true;
    }