Join Regular Classroom : Visit ClassroomTech

Anagrams | Coding Problem Solve | codewindow.in

Anagram

An anagram is a word, phrase or name formed by rearranging the letters of another word, phrase or name.
Write a function to check if two given strings are anagrams or not. Return “yes” if they are anagrams, otherwise return “no”.

Input Specifications:
input1: the first string
input2: the second string

Output Specification:
Return “yes” if they are anagrams, otherwise, return “no”.

Example 1:
input1: build
input2: dubli

Ouput: yes

Explanation:
The first string can be rearranged to form the second string. Hence, they are anagram of each other.

Example 2:
input1: beast
input2: yeast

Output: no

Explanation:
The first string contains the letter ‘b’ which is not present in the second string. Similarly second string contains the letter ‘y’ which is not
present in the first string. Hence, the two strings are not anagrams of each other.

Coding Solve:

// Solution in Java
import java.util.*;
import java.lang.*;
import java.io.*;

class Codewindow
{
    static String sortString(String str) {
        char tempStrArr[] = str.toCharArray();
        Arrays.sort(tempStrArr);
        return new String(tempStrArr);
    }
    
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str1 = sortString(br.readLine());
		String str2 = sortString(br.readLine());
		if(str1.equals(str2))
		    System.out.println("yes");
		else
    		System.out.println("no");
	}
}
Recent Posts