-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallenge.java
More file actions
69 lines (60 loc) · 2.32 KB
/
Challenge.java
File metadata and controls
69 lines (60 loc) · 2.32 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
package c284;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/*
* https://www.reddit.com/r/dailyprogrammer/comments/53ijnb/20160919_challenge_284_easy_wandering_fingers/
*/
public class Challenge {
private static final String INPUT1 = "qwertyuytresdftyuioknn";
private static final String INPUT2 = "gijakjthoijerjidsdfnokg";
public static void main(String[] args) throws Exception {
Collection<String> matches1 = findWords(INPUT1);
Collection<String> matches2 = findWords(INPUT2);
System.out.println("INPUT: " + INPUT1);
System.out.println("MATCHES: " + matches1);
System.out.println("INPUT: " + INPUT2);
System.out.println("MATCHES: " + matches2);
}
private static Collection<String> findWords(final String input) throws Exception {
Collection<String> res = new ArrayList<>();
res = readSuppliedWords();
// filter for potential matches (first and last letter matches)
res = res.stream().filter(c -> c.length() >= 5)
// check first char
.filter(c -> c.startsWith(input.substring(0, 1)))
// check last char
.filter(c -> c.endsWith(input.substring(input.length() - 1, input.length())))
// check if the char sequence matches to the input
.filter(c -> charSequenceMatch(input, c))
.collect(Collectors.toList());
return res;
}
private static boolean charSequenceMatch(final String input, final String toMatch) {
boolean res = true;
int inputCursor = 0;
final char[] toMatchChr = toMatch.toCharArray();
for (int i = 0; i < toMatchChr.length; i++) {
final int foundIdx = input.indexOf(toMatchChr[i], inputCursor);
// when the current char cannot be found after the current cursor, end it
if(foundIdx == -1 || foundIdx < inputCursor) {
res = false;
break;
}
inputCursor = foundIdx;
}
return res;
}
private static Collection<String> readSuppliedWords() throws FileNotFoundException {
InputStream input = Challenge.class.getClassLoader().getResourceAsStream("c284/words.txt");
List<String> res = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines()
.collect(Collectors.toList());
return res;
}
}