-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay39_prob1.java
63 lines (49 loc) · 1.55 KB
/
Day39_prob1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Write a program to check whether two strings are ANAGRAM or not. Strings will be anagram if they have same set of characters same number of times. Your program should take the input of two strings of length greater than 2. If the length of the any string is less than equal 2 then display the message “Invalid Input” without taking any more input.
Input Format
Your program should take the input of two strings.
Constraints
Length of strings should be greater than 2.
Output Format
Your program should display the “ANAGRAM” or “NOT ANAGRAM” depending on the input strings.
Sample Input 0
silent
listen
Sample Output 0
ANAGRAM
Sample Input 1
ssilent
listenn
Sample Output 1
NOT ANAGRAM
*/
Kirtan Jain
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
char[] s1 = sc.nextLine().toCharArray();
char[] s2 = sc.nextLine().toCharArray();
int flag = 0;
Arrays.sort(s1);
Arrays.sort(s2);
if(s1.length<=2 || s2.length<=2){
System.out.println("Invalid Input");
return;
}
for(int i =0;i<s1.length;i++){
if(s1[i]!=s2[i]){
flag=1;
break;
}
}
if(flag==0){
System.out.println("ANAGRAM");
}
else{
System.out.println("NOT ANAGRAM");
}
}
}