-
Notifications
You must be signed in to change notification settings - Fork 0
/
3Scrabble.java
45 lines (37 loc) · 1.08 KB
/
3Scrabble.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
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
public class CountDown{
public static void main(String[] args){
FileIO reader = new FileIO();
String[] contents = reader.load("C:\\dictionary.txt");
Scanner myScanner = new Scanner(System.in);
System.out.println("Enter the string of random letters");
String input = myScanner.nextLine();
int biggest = 0;
ArrayList<String> myList = new ArrayList<String>();
for (int i = 0; i< contents.length; i++){
int length =
check(input,contents[i].substring(0,contents[i].length()-1));
if(length>biggest){
biggest=length;
myList.clear();
myList.add(contents[i]);
}else if(length==biggest){
myList.add(contents[i]);
}
}
System.out.println("The longest word that can be formed is one
with "+biggest+" letters:");
for(int i=0;i<myList.size();i++){
System.out.println(myList.get(i));
}
}
public static int check(String letters, String word){
for(int i=0; i<word.length(); i++){
if(!letters.contains(""+word.charAt(i))){
return -1;
}
letters=letters.replaceFirst(""+word.charAt(i)," ");
}
return word.length();
}
}