forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInefficient.java
More file actions
31 lines (26 loc) · 853 Bytes
/
Inefficient.java
File metadata and controls
31 lines (26 loc) · 853 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
30
31
/**
* Version of Doubloon that uses nested loops.
*/
public class Inefficient {
public static boolean isDoubloon(String s) {
// count the number of times each letter appears
s = s.toLowerCase();
for (char c = 'a'; c <= 'z'; c++) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
count++;
}
}
// determine whether the word is a doubloon
if (count != 0 && count != 2) {
return false; // not a doubloon
}
}
return true; // all counts were 0 or 2
}
public static void main(String[] args) {
System.out.println(isDoubloon("Mama"));
System.out.println(isDoubloon("Lama"));
}
}