-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathIsogramChecker.java
More file actions
45 lines (38 loc) · 1.14 KB
/
IsogramChecker.java
File metadata and controls
45 lines (38 loc) · 1.14 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
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Determine if a word or phrase is an isogram.
*
* An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter,
* however spaces and hyphens are allowed to appear multiple times.
*
* Examples of isograms:
*
* lumberjacks
* background
* downstream
* six-year-old
*
* The word isograms, however, is not an isogram, because the s repeats.
*/
class IsogramChecker {
boolean isIsogram(String phrase) {
phrase = removeSpacesAndHypens(phrase);
return !checkForDoubleChars(phrase);
}
private Boolean checkForDoubleChars(String phrase) {
String partPhraseToTest = phrase.toLowerCase(Locale.ROOT);
for (char charUnderTest : partPhraseToTest.toCharArray()) {
partPhraseToTest = partPhraseToTest.substring(1);
boolean charFoundInPartPhrase = partPhraseToTest.indexOf(charUnderTest) != -1;
if(charFoundInPartPhrase) {
return true;
}
}
return false;
}
private String removeSpacesAndHypens(String phrase) {
return phrase.replace(" ", "").replace("-", "");
}
}