-
Notifications
You must be signed in to change notification settings - Fork 2
/
39.1 String is ANAGRAM or NOT ANAGRAM.java
96 lines (79 loc) · 2.49 KB
/
39.1 String is ANAGRAM or NOT ANAGRAM.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
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
*/
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);
String s1 = sc.next();
String s2 = sc.next();
if(s1.length()<=2 || s2.length()<=2)
{
System.out.print("Invalid Input");
}
else if(s1.length()!=s2.length())
{
System.out.print("NOT ANAGRAM");
}
else
{
char []c1 = s1.toCharArray();
char []c2 = s2.toCharArray();
boolean check = true;
for(int i=0;i<c1.length;i++)
{
int count1 = 0,count2 = 0;
if(c1[i]=='$')
continue;
char key = c1[i];
for(int j=0;j<c1.length;j++)
{
if(c1[j]=='$')
continue;
if(c1[j]==key)
{
count1++;
c1[j] = '$';
}
}
for(int k=0;k<c2.length;k++)
{
if(c2[k]=='$')
continue;
if(c2[k]==key)
{
count2++;
c2[k] = '$';
}
}
if(count1!=count2)
{
check = false;
break;
}
}
if(check)
System.out.print("ANAGRAM");
else
System.out.print("NOT ANAGRAM");
}
}
}