forked from ApolloZhu/Think-Java-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.8.java
32 lines (30 loc) · 808 Bytes
/
9.8.java
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
//
// 9.8.java
// Cahpter9
//
// Created by Apollo Zhu on 8/11/16.
// Copyright © 2015-2016 WWITDC. All rights reserved.
//
public static int[] letterHist(String str){
char[] characters = str.toUpperCase().toCharArray();
int[] result = new int[27];
for (char letter:characters){
if (letter >= 65 && letter <= 90){
result[letter-65]+=1;
}
else if (letter != 32){
result[27]+=1;
}
}
return result;
}
public static boolean canSpell(String word, String libchar){
int[] wordRequirment = letterHist(word);
int[] libCapacity = letterHist(libchar);
for (int i=0;i<26;i++){
if (wordRequirment[i] > libCapacity[i]){
return false;
}
}
return true;
}