-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagramProgram.java
More file actions
69 lines (53 loc) · 1.41 KB
/
AnagramProgram.java
File metadata and controls
69 lines (53 loc) · 1.41 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
package com.interview;
import java.util.Arrays;
public class AnagramProgram {
public AnagramProgram() {
// TODO Auto-generated constructor stub
}
public static void checkStringAnagramOrNot() {
String str2 = "race";
String str3 = "care";
// Check if the strings are the same length
if (str2.length() != str3.length()) {
System.out.println("The strings are not anagrams.");
return;
}
// Convert the strings to lower case
str2 = str2.toLowerCase();
str3 = str3.toLowerCase();
// Sort the strings
char[] arr1 = str2.toCharArray();
char[] arr2 = str3.toCharArray();
// Sort the arrays
Arrays.sort(arr1);
Arrays.sort(arr2);
// Check if the sorted strings are equal
if (Arrays.equals(arr1, arr2)) {
System.out.println("The strings are anagrams.");
} else {
System.out.println("The strings are not anagrams.");
}
}
public static void main(String[] args) {
String str = "abc";
String str1 = "acb";
char ch[] = str.toCharArray();
char ch1[] = str1.toCharArray();
boolean flag = false;
for (int i = 0; i < ch.length; i++) {
for (int j = i + 1; j < ch1.length; j++) {
if (ch[i] == ch1[j]) {
flag = true;
} else {
flag = false;
}
}
}
if (flag == true) {
System.out.println("String are anagram ");
} else {
System.out.println("String are not anagram ");
}
// ========================2nd Way=================================
}
}