forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubloon.java
More file actions
29 lines (25 loc) · 785 Bytes
/
Doubloon.java
File metadata and controls
29 lines (25 loc) · 785 Bytes
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
/**
* Example from the end of Chapter 7.
*/
public class Doubloon {
public static boolean isDoubloon(String s) {
// count the number of times each letter appears
int[] counts = new int[26];
String lower = s.toLowerCase();
for (char letter : lower.toCharArray()) {
int index = letter - 'a';
counts[index]++;
}
// determine whether the given word is a doubloon
for (int count : counts) {
if (count != 0 && count != 2) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isDoubloon("Mama")); // true
System.out.println(isDoubloon("Lama")); // false
}
}