Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Sunday 24 March 2019

The Interview Question Today#17(12-3-19)

Question:

Write a Program to check whether two given strings are an anagram of each other or not. 

Example:

Input: String1: silent

           String2: listen

Output: The two strings are an anagram of each other


Solution:

Approach 1:

1) Create count arrays of size 256 for both strings. Initialize all values in count arrays as 0.
2) Iterate through every character of both strings and increment the count of character in the corresponding count arrays.

3) Compare count arrays. If both count arrays are the same, then return true.


Approach 2:

1)Compare the length of both strings if equal continue or else false and exit.
2) Sorting Both Strings.
3) Comparing them.
4) if equal then true or false.

C program://Approach 1

#include <stdio.h>
#define NO_OF_CHARS 256

/* function to check whether two strings are an anagram of each other */
int areAnagram(char* str1, char* str2)
{
    int count1[NO_OF_CHARS] = { 0 };
    int count2[NO_OF_CHARS] = { 0 };
    int i;

    for (i = 0; str1[i] && str2[i]; i++) {
        count1[str1[i]]++;
        count2[str2[i]]++;
    }
   if (str1[i] || str2[i])
        return 0;

    for (i = 0; i < NO_OF_CHARS; i++)
        if (count1[i] != count2[i])
            return 0;

    return 1;
}
int main()
{
    char str1[100];
    char str2[100];
    printf("Enter the First String: ");
    scanf("%s",str1);
    printf("Enter the Second String: ");
    scanf("%s",str2);

    if (areAnagram(str1, str2))
        printf("The two strings are an anagram of each other");
    else
        printf("The two strings are not an anagram of each other");

    return 0;

}

Output:

Enter the First String: amit
Enter the Second String: tima

The two strings are an anagram of each other



C program: //Appoarch 2.

#include <stdio.h>
#include<stdlib.h>
#include<string.h>

void main()
{
    printf("Enter 2 strings ");
    char s1[30],s2[30];
    scanf("%s",s1);
    scanf("%s",s2);
    int m=strlen(s1);
    int n=strlen(s2);
    if(m!=n){// Comparing the Length.
        printf("Strings are not anagram case 1\n");
        exit(0);
    }
//Sorting Both Array using Bubble Sort.
    int i,j,flag=0;
   char temp;
   for(i=0;i<m-1;i++)
  {
      for(j=0;j<n-i-1;j++)
        if(s1[j]>s1[j+1])
        {   temp=s1[j];
            s1[j]=s1[j+1];
            s1[j+1]=temp;
        }
  }

    for(i=0;i<m-1;i++)
     {

        for(j=0;j<n-i-1;j++)
            if(s2[j]>s2[j+1])
            {   //printf("Hello");
                temp=s2[j];
                s2[j]=s2[j+1];
                s2[j+1]=temp;
        }
    }



//Comparing Both The Array.
    for(i=0;i<n;i++){
        if(s1[i]!=s2[i])
            flag=2;
    }
    if(flag==2)
        printf("Strings are not anagram\n");
    else
        printf("Strings are anagram\n");


}

Java: Approach 2
import java.util.Arrays;
import java.util.Scanner;

/*
 * A way to check if two Strings are anagram or not in Java
 * return true, if both Strings are anagram. 
 * else false, 
 * And it is case Sensitive 
 @JavaAbhigyan
 */

public class anagram {
private static Scanner s;
private static String str1,str2;
private static boolean ans;
public static boolean iAnagram(String word, String anagram)
{
char[] charFromWord = word.toCharArray();
char[] charFromAnagram = anagram.toCharArray();
Arrays.sort(charFromWord); Arrays.sort(charFromAnagram);

return Arrays.equals(charFromWord, charFromAnagram); 
}
public static void main(String[] args) {
s = new Scanner(System.in);
System.out.println("Enter the Main String\n");
str1 = s.nextLine();
System.out.println("Enter the test String\n");
    str2=s.nextLine();
ans = iAnagram(str1, str2);
System.out.println("Enter String:"+str1+" And String:"+str2+" are Anagram:"+ans);
}

}


0 comments:

Post a Comment