Skip to content

Instantly share code, notes, and snippets.

@ibanez270dx
Created March 11, 2019 21:57
Show Gist options
  • Save ibanez270dx/bf9e2159beed59b3a43ac9d753b7fc74 to your computer and use it in GitHub Desktop.
Save ibanez270dx/bf9e2159beed59b3a43ac9d753b7fc74 to your computer and use it in GitHub Desktop.
100 points interview
import java.util.Map;
import java.util.HashMap;
import java.lang.Character;
public class points {
private static Map<Character, Integer> pointsMap = null;
private static void populateMap() {
pointsMap.put('a', 1);
pointsMap.put('b', 2);
pointsMap.put('q', 17);
pointsMap.put('u', 21);
pointsMap.put('r', 18);
pointsMap.put('t', 20);
pointsMap.put('e', 5);
// z 26, y 25, x 24, w 23, v 22, u 21, t 20, s 19, r 18, q 17
// ...
}
public static void main(String[] args) {
if (pointsMap == null) {
pointsMap = new HashMap(26);
populateMap();
}
points p = new points();
System.out.println("100 Points in \"quarter\" " + p.isOneHundred("quarter"));
System.out.println("100 Points in \"hello\" " + p.isOneHundred("hello"));
System.out.println("100 Points in \"\" " + p.isOneHundred(""));
}
public points points() {
// if (pointsMap == null) {
// pointsMap = new HashMap(26);
// populateMap();
// }
return points();
}
public boolean isOneHundred(String str) {
int sum = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
// Find value
Integer value = pointsMap.get(c);
if (value == null) {
continue;
}
// Add to sum
sum += value;
if (sum > 100) {
return false;
}
}
if (sum == 100) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment