Join Regular Classroom : Visit ClassroomTech

Accenture Coding Question | Anagrams | CodeWindow

Anagrams

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 the given two strings are anagrams or not. return “Yes” if they are anagrams otherwise return “No”.

Input specification:
Input1: The first string.
Input2: The second string.

Output Specification:
return “Yes” if they are anagrams otherwise return “No”.

Example 1:
Input 1: build
Input 2: dubli

Output: Yes
Explanation:
build and dubli are anagrams

Example 2:
Input 1: abcde
Input 2: bcdef

Output: No
Explanation:
abcde and bcdef are not anagrams

Solution in Python 3:

#https://codewindow.in
#join our telegram channel

n=input()
m=input()
count=0
l=len(m)
for i in n:
    if i in m:
        count=count+1
    else:
        pass
    
if count == l:
    print("Yes")
else:
    print("No")
    
# Telegram @codewindow

Solution in C++ :

//https://codewindow.in
//join our telegram channel @codewindow

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

bool areAnagram(string str1, string str2)
{
    int n1 = str1.length();
    int n2 = str2.length();
 
    if (n1 != n2)
        return false;

    sort(str1.begin(), str1.end());
    sort(str2.begin(), str2.end());
    for (int i = 0; i < n1; i++)
        if (str1[i] != str2[i])
            return false;
 
    return true;
}

int main()
{
    string str1;
    string str2;
    cin >> str1;
    cin >> str2;

    if (areAnagram(str1, str2))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Solution in JAVA :

//https://codewindow.in
//join our telegram channel @codewindow

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    static boolean areAnagram(char[] str1, char[] str2)
    {
        int n1 = str1.length;
        int n2 = str2.length;
 
        if (n1 != n2)
            return false;
        
        Arrays.sort(str1);
        Arrays.sort(str2);
 
        for (int i = 0; i < n1; i++)
            if (str1[i] != str2[i])
                return false;
 
        return true;
    }
 
    public static void main(String args[]) throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str1 = br.readLine();
        String str2 = br.readLine();
       
        if (areAnagram(str1.toCharArray(), str2.toCharArray()))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}

Output:

abc
aabbc
No
build
dubli
Yes
Recent Posts
Pages