-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathScrabble.java
More file actions
150 lines (131 loc) · 4.98 KB
/
Scrabble.java
File metadata and controls
150 lines (131 loc) · 4.98 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.booleanuk;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/*
My thoughts:
The method for validating brackets should probably have been better planned. Felt like I followed the tests and
hard-coded conditions a bit too much. The solution was also used completely without use of internet or AI.
After some research I have found that using a stack implemented with an ArrayDeque could have been a better
way of solving the problem.
*/
public class Scrabble {
String word;
public Scrabble(String word) {
this.word = word;
}
public int score() {
Map<Character, Integer> scoreMap = getCharacterIntegerMap();
// Validate the input
if (!isValidBracketSymmetry(word)) {
return 0;
}
String wordUppercase = word.toUpperCase();
int sum = 0;
int multiplier = 1;
int startIndex = 0;
int endIndex = wordUppercase.length();
// Full word multiplication
if (wordUppercase.startsWith("{") && wordUppercase.endsWith("}") &&
notFakeFullWrap(wordUppercase, '}')) {
multiplier = 2;
startIndex++;
endIndex--;
if (wordUppercase.charAt(1) == '[' && wordUppercase.charAt(endIndex - 1) == ']') {
multiplier = 6; // Full word wrapped in both curly and square brackets
startIndex++;
endIndex--;
}
} else if (wordUppercase.startsWith("[") && wordUppercase.endsWith("]") &&
notFakeFullWrap(wordUppercase, ']')) {
multiplier = 3;
startIndex++;
endIndex--;
}
// Iterate through word
for (int i = startIndex; i < endIndex; i++) {
char c = wordUppercase.charAt(i);
if (c == '{' || c == '[') {
// Letter curly brackets -> next letter double score
int letterMultiplier = (c == '{') ? 2 : 3;
sum += scoreMap.get(wordUppercase.charAt(i + 1)) * letterMultiplier * multiplier;
i += 2;
} else {
sum += scoreMap.get(c) * multiplier;
}
}
// Return score
return sum;
}
private boolean isValidBracketSymmetry(String word) {
if (word.length() < 3 && word.matches("^[a-zA-Z]+$")) {
return true;
}
if (word.isEmpty() || !word.matches("^[a-zA-Z{}\\[\\]]*$")) {
return false;
}
int first = word.charAt(0);
int last = word.charAt(word.length() - 1);
int third = word.charAt(2);
int thirdLast = word.charAt(word.length() - 3);
// if the full word is wrapped, remove these brackets to check the rest
if (((first == '{' && last == '}') && third != '}' && thirdLast != '{') || ((first == '[' && last == ']') && third != ']' && thirdLast != '[')) {
word = word.substring(1, word.length() - 1);
}
first = word.charAt(0);
last = word.charAt(word.length() - 1);
if ((first == '[' && third != ']') && (last == ']' && thirdLast != '[')) {
word = word.substring(1, word.length() - 1);
}
// loop through the rest to check for incorrect use of brackets
for (int i = 0; i < word.length() - 2; i++) {
if (word.charAt(i) == '{' && word.charAt(i + 2) != '}') {
return false;
}
if (word.charAt(i) == '[' && word.charAt(i + 2) != ']') {
return false;
}
if ((word.charAt(i) == ']' && i < 2) || (word.charAt(i) == '}' && i < 2)) {
return false;
}
}
for (int i = word.length() - 1; i > 2; i--) {
if (word.charAt(i) == '}' && word.charAt(i - 2) != '{') {
return false;
}
if (word.charAt(i) == ']' && word.charAt(i - 2) != '[') {
return false;
}
}
return true;
}
private boolean notFakeFullWrap(String word, char close) {
int closingPos = word.indexOf(close);
return closingPos == word.length() - 1;
}
private static Map<Character, Integer> getCharacterIntegerMap() {
Map<Character, Integer> scoreMap = new HashMap<>();
for (char c : new char[]{'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'}) {
scoreMap.put(c, 1);
}
for (char c : new char[]{'D', 'G'}) {
scoreMap.put(c, 2);
}
for (char c : new char[]{'B', 'C', 'M', 'P'}) {
scoreMap.put(c, 3);
}
for (char c : new char[]{'F', 'H', 'V', 'W', 'Y'}) {
scoreMap.put(c, 4);
}
for (char c : new char[]{'K'}) {
scoreMap.put(c, 5);
}
for (char c : new char[]{'J', 'X'}) {
scoreMap.put(c, 8);
}
for (char c : new char[]{'Q', 'Z'}) {
scoreMap.put(c, 10);
}
return scoreMap;
}
}