-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathIsogramChecker.java
More file actions
48 lines (43 loc) · 1.16 KB
/
IsogramChecker.java
File metadata and controls
48 lines (43 loc) · 1.16 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
import java.util.Arrays;
/**
* 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) {
String sortedPhrase = stringSortAlphabet(phrase.toUpperCase());
char[] charSortedPhrase = sortedPhrase.toCharArray();
char testingLetter = '$';
boolean status = false;
if (charSortedPhrase.length >1){
for (char letter : charSortedPhrase) {
if (testingLetter == letter && (letter !=' ' && letter !='-')){
status = false;
break;
}else {
status = true;
}
testingLetter = letter;
}
}else {
status = true;
}
return status;
}
private String stringSortAlphabet(String word){
char[] stringToChar = word.toCharArray();
Arrays.sort(stringToChar);
return new String(stringToChar);
}
}