forked from divScorp/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram.java
More file actions
72 lines (65 loc) · 1.58 KB
/
Anagram.java
File metadata and controls
72 lines (65 loc) · 1.58 KB
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
package String;
/*
An anagram of a string is another string that contains same characters,
only the order of characters can be different.
for example -
"Mother in law" and "Hitler Woman" are anagram.
*/
import java.util.Scanner;
public class Anagram {
public static String lowerCase(String s) {
char[] c = s.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] >= 'a' && c[i] <= 'z') {
c[i] -= 32;
}
}
return new String(c);
}
public static String removeSpace(String s) {
String t = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ')
t += s.charAt(i);
}
return t;
}
public static String sort(String s) {
char[] c = s.toCharArray();
for (int i = 0; i < c.length - 1; i++)
for (int j = i + 1; j < c.length; j++) {
if (c[i] > c[j]) {
char t = c[i];
c[i] = c[j];
c[j] = t;
}
}
return new String(c);
}
public static boolean checkAnagram(String f, String s) {
String s1 = removeSpace(f);
String s2 = removeSpace(s);
if (s1.length() != s2.length()) {
return false;
}
s1 = sort(s1);
s2 = sort(s2);
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i))
return false;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two String: ");
String first = sc.nextLine();
String second = sc.nextLine();
if(checkAnagram(lowerCase(first), lowerCase(second))){
System.out.println(" Strings are anagram!! ");
}else {
System.out.println(" Strings is not anagram!! ");
}
sc.close();
}
}