-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anagram.java
69 lines (47 loc) · 1.53 KB
/
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
/*Write a program to check if two strings are anagrams of each other. An anagram is a word or phrase formed by rearranging
the letters of another word or phrase. Your program should return True if the two input strings are anagrams, and False
otherwise.
For example, if the input strings are "listen" and "silent", the program should return True, as both strings can be rearranged
to form the same letters.
Write a program that takes two strings as input and determines whether they are anagrams or not.
Sample input1:
----------------
listen
silent
Sample output1:
---------------
True
Sample input2:
---------------
hello
world
Sample output2:
---------------
False
*/
import java.util.Scanner;
public class Anagram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("ENter Str 1");
String str1 = sc.nextLine();
char[] ch1 = str1.toCharArray();
System.out.println("ENter Str 2");
String str2 = sc.nextLine();
char[] ch2 = str1.toCharArray();
int check = 0;
for (int i = 0; i < ch1.length; i++) {
for (int j = 0; j < ch2.length; j++) {
if(ch1[i] == ch2[j]){
check++;
break;
}
}
}
if (ch1.length == ch2.length && check == ch1.length) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}