Skip to main content

Command Palette

Search for a command to run...

Anagram

Published
2 min read
D

Hello everyone, I'm Dhanraj, a CSIT Btech student currently pursuing my degree at Rajarambapu Institute of Technology. I'm a passionate web developer and Java programmer, with a keen interest in data structures and algorithms.

Programming has always been my passion since my school days, and I have been constantly exploring the world of computer science. My fascination with web development began when I discovered the power of the internet, and since then, I have been honing my skills to create beautiful, functional websites.

I believe that perseverance and hard work are the keys to success in any field, and programming is no exception. I enjoy the challenge of solving complex problems and take pride in delivering high-quality code that meets the requirements of my clients.

Apart from programming, I also have a keen interest in playing chess and expanding my knowledge of the stock market. I strongly believe that a well-rounded personality is crucial for success in life, and I always strive to balance my academic pursuits with my extracurricular activities.

I'm excited to share my experiences and knowledge with you through my blog. Whether you're a beginner or an experienced programmer, I hope that my articles will inspire you to push your limits and reach new heights in your programming journey.

Hello coders, today I have solved a DSA problem on string manipulation that involves finding anagrams. An anagram is a word or phrase formed by rearranging the letters of another word or phrase, typically using all the original letters exactly once. For example, "silent" is an anagram of "listen". Here is the problem statement and my solution code for it. I hope you find it easy to understand.

Problem: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, act and tac are an anagram of each other.

Note:-

  • If the strings are anagrams you have to return True or else return False

  • |s| represents the length of string s.

Example 1:

Input:a = geeksforgeeks, b = forgeeksgeeks
Output: YES
Explanation: Both the string have same characters with
        same frequency. So, both are anagrams.

Example 2:

Input:a = allergy, b = allergic
Output: NO
Explanation: Characters in both the strings are 
        not same, so they are not anagrams.

Your Task:
You don't need to read input or print anything. Your task is to complete the function isAnagram() which takes the string a and string b as input parameter and check if the two strings are an anagram of each other. The function returns true if the strings are anagram else it returns false.

**Expected Time Complexity:**O(|a|+|b|).
Expected Auxiliary Space: O(Number of distinct characters).

Constraints:
1 ≤ |a|,|b| ≤ 105

Solution :

 public static boolean isAnagram(String a,String b)
    {

       char[] c = a.toCharArray();
       char[] d =b.toCharArray();

        Arrays.sort(c);
        Arrays.sort(d);

        if(Arrays.equals(c,d))
            return true;
        else
            return false;

    }

Output :

Input:a = geeksforgeeks, b = forgeeksgeeks
Output: YES
Explanation: Both the string have same characters with
        same frequency. So, both are anagrams.

More from this blog

DHANRAJ NIKAM's blog

7 posts