-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanagramCheck.java
More file actions
64 lines (52 loc) · 2.04 KB
/
PanagramCheck.java
File metadata and controls
64 lines (52 loc) · 2.04 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
package org.simplemedium;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class PanagramCheck {
public static void main(String[] args) {
String[] sentences = {
"The quick brown fox jumps over the lazy dog",
/* "Pack my box with five dozen liquor jugs",
"Jinxed wizards pluck ivy from the big quilt",*/
"this is probably not a panagram xyz qer kuwv"
};
List<Character> missingChars = new ArrayList<>();
for(String str: sentences) {
//missingChars = isPanagram(str);
Set<Character> missingCharsSet = findMissingChars(str);
//if(missingChars.isEmpty()) {
if(missingCharsSet.isEmpty()) {
System.out.println("The following string is a panagram: \r\n " + str);
}
else {
System.out.println("The following string is a not a panagram: \r\n " + str);
System.out.println("The missing characters are : " + missingCharsSet.toString());
}
}
}
public static List<Character> isPanagram(String str) {
String strLower = str.toLowerCase().replaceAll("[^a-z]", "");
List<Character> missingChars = new ArrayList<>();
for(char c='a'; c<='z'; c++) {
if(!strLower.contains(Character.toString(c))) {
missingChars.add(c);
}
}
return missingChars;
}
public static Set<Character> findMissingChars(String str) {
String strLower = str.toLowerCase().replaceAll("[^a-z]", "");
Set<Character> alphabetSet = new HashSet<>();
for (char c = 'a'; c <= 'z'; c++) {
alphabetSet.add(c);
}
Set<Character> presentChars = new HashSet<>();
for (char c : strLower.toCharArray()) {
presentChars.add(c);
}
Set<Character> missingChars = new HashSet<>(alphabetSet);
missingChars.removeAll(presentChars);
return missingChars;
}
}