Skip to content

Commit 1d6e663

Browse files
committed
Added interview coding solutions - under #1
1 parent 0f12eea commit 1d6e663

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
3+
Q: Write a java program to check whether two strings are anagram or not.
4+
Explaination: An anagram is a word, phrase, or name formed by rearranging the letters of another, such as cinema, formed from iceman.
5+
6+
Sample Input: Listen, Silent
7+
Sample Output: Anagram
8+
9+
Sample Input: Tic, Tac
10+
Sample Output: Not Anagram
11+
12+
*/
13+
14+
import java.util.Scanner;
15+
import java.util.Arrays;
16+
17+
public class Anagram
18+
{
19+
public static void main(String[] args) {
20+
Scanner scan = new Scanner(System.in);
21+
System.out.println("Enter first string:");
22+
String str1 = scan.nextLine();
23+
System.out.println("Enter second string:");
24+
String str2 = scan.nextLine();
25+
// Initially checking if the length of both strings are same or not if not then printing not anagram and exiting the program
26+
if(str1.length() != str2.length()) {
27+
System.out.println("Not Anagram");
28+
System.exit(0);
29+
}
30+
//If the length of both strings are same then converting both strings to lower case character array and sorting them
31+
char []arr1 = str1.toLowerCase().toCharArray();
32+
char []arr2 = str2.toLowerCase().toCharArray();
33+
Arrays.sort(arr1);
34+
Arrays.sort(arr2);
35+
//Comparing the sorted arrays and printing the result
36+
if(Arrays.equals(arr1, arr2)) {
37+
System.out.println("Anagram");
38+
}
39+
else {
40+
System.out.println("Not Anagram");
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)