-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathANAGRAMCheck_in_java.txt
More file actions
75 lines (63 loc) · 2.19 KB
/
ANAGRAMCheck_in_java.txt
File metadata and controls
75 lines (63 loc) · 2.19 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
73
74
75
//Check whether Two strings are ANAGRAM or Not
package com.vishal.src;
import java.util.*;
public class AnagramChecker {
public boolean checkAnagram(String s1, String s2){
boolean flag=false;
/*------------------------------Check String1 & string2 size---------------------------------*/
if(s1.isEmpty() || s2.isEmpty()){
System.out.println("One of the string is blank, can't check anagram as condition not fullfilled");
}
else if(s1.length()!=0 || s2.length()!=0){
if(s1.length()<s2.length()){
System.out.println("Strings are not Anagram string-1 size is less then string-2");
}
else if(s1.length()>s2.length()){
System.out.println("Strings are not Anagram string-1 size is more then string-2");
}
else{
/*------------------------------s1 String---------------------------------*/
Character[] c1= new Character[s1.length()];
for(int i=0; i<c1.length; i++){
c1[i]=s1.charAt(i);
}
ArrayList<Character> list1 = new ArrayList<Character>(Arrays.asList(c1));
System.out.println(list1);
/*------------------------------s2 String---------------------------------*/
Character[] c2= new Character[s2.length()];
for(int i=0; i<c2.length; i++){
c2[i]=s2.charAt(i);
}
ArrayList<Character> list2 = new ArrayList<Character>(Arrays.asList(c2));
System.out.println(list2);
/*------------------------------logic for checking anagram if strings size are same---------------------------------*/
for(Character ctr: list1){
if(list2.contains(ctr)){
list2.remove(ctr);
}
}
if(list2.isEmpty()){
flag=true;
}else
flag=false;
System.out.println(list1+" "+list2);
}
}
return flag;
}
public static void main(String[] args) {
String s1= "dove";
String s2= "dove";
AnagramChecker anagramcheck = new AnagramChecker();
boolean flag = anagramcheck.checkAnagram(s1, s2);
if(flag==true){
System.out.println("Strings are Anagram "+s1+" "+s2);
}
else
System.out.println("Strings are not Anagram");
}
}
Output:- [d, o, v, e]
[d, o, v, e]
[d, o, v, e] []
Strings are Anagram dove dove