{"id":888,"date":"2015-11-18T00:45:34","date_gmt":"2015-11-18T05:45:34","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=888"},"modified":"2015-11-18T00:50:41","modified_gmt":"2015-11-18T05:50:41","slug":"scrabble-problem","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2015\/11\/18\/scrabble-problem\/","title":{"rendered":"Scrabble Problem"},"content":{"rendered":"<p>If I give you scrabble tiles and a dictionary, determine the valid words<br \/>\nfrom your collection of tiles.<\/p>\n<p><code><\/p>\n<pre lang='java'>package joshho;\r\n\r\nimport java.io.BufferedReader;\r\nimport java.io.FileReader;\r\nimport java.io.IOException;\r\nimport java.util.Arrays;\r\n\r\npublic class Runner {\r\n\tScrabbleSet set ;\r\n\t\r\n\tprivate class ScrabbleSet {\r\n\t\tchar[] chars;\r\n\t\tpublic ScrabbleSet(String[] orig){\r\n\t\t\tchars = new char[orig.length];\r\n\t\t\tfor(int i=0;i<orig.length;i++) \r\n\t\t\t\tchars[i] = orig[i].charAt(0);\r\n\t\t\tArrays.sort(chars);\r\n\t\t}\r\n\r\n\t\tpublic boolean checkWord(char[] find){\r\n\t\t\tif(find.length > chars.length) return false;\r\n\t\t\tArrays.sort(find);\r\n\t\t\tint s=0;\r\n\t\t\tfor(int i=0;i<find.length;i++){\r\n\t\t\t\tfor(int j=s;j<chars.length;j++){\r\n\t\t\t\t\tif(find[i] > chars[j]) continue;\r\n\t\t\t\t\tif(find[i] == chars[j]){\r\n\t\t\t\t\t\ts=j+1;\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t}\r\n\t\t\t\t\treturn false;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn true;\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic void load(String y){\r\n\t\tset = new ScrabbleSet(y.split(\" \"));\r\n\t}\r\n\r\n\tpublic static void main(String[] args) throws IOException {\r\n\t\targs = new String[]{\"Scrabble\\\\data\\\\english4000.dic\",\r\n\t\t\t\t\"g w x b d u t z q m o i e p o\"};\r\n\t\tFileReader fr =  new FileReader(args[0]);\r\n\t\tBufferedReader br = new BufferedReader(fr);\r\n\r\n\t\tRunner r = new Runner();\r\n\t\tr.load(args[1]);\r\n\t\tSystem.out.println(args[1]);\r\n\t\t\r\n\t\tString line;\r\n\t\twhile((line = br.readLine()) != null) {\r\n\t\t\tSystem.out.print(line+\" \");\r\n\t\t\tSystem.out.println(r.set.checkWord(line.toCharArray()));\r\n\t\t}\r\n\t\tbr.close();\r\n\t\t\r\n\t\t\r\n\t}\r\n\r\n}<\/pre>\n<p><\/code><\/p>\n<p>There&#8217;s probably a better way to do this, I am interested<br \/>\nto see what other solutions there are.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If I give you scrabble tiles and a dictionary, determine the valid words<br \/>\nfrom your collection of tiles.<\/p>\n<p><code><\/p>\n<pre lang='java'>package joshho;\r\n\r\nimport java.io.BufferedReader;\r\nimport java.io.FileReader;\r\nimport java.io.IOException;\r\nimport java.util.Arrays;\r\n\r\npublic class Runner {\r\n\tScrabbleSet set ;\r\n\t\r\n\tprivate class ScrabbleSet {\r\n\t\tchar[] chars;\r\n\t\tpublic ScrabbleSet(String[] orig){\r\n\t\t\tchars = new char[orig.length];\r\n\t\t\tfor(int i=0;i<orig.length;i++) \r\n\t\t\t\tchars[i] = orig[i].charAt(0);\r\n\t\t\tArrays.sort(chars);\r\n\t\t}\r\n\r\n\t\tpublic boolean checkWord(char[] find){\r\n\t\t\tif(find.length > chars.length) return false;\r\n\t\t\tArrays.sort(find);\r\n\t\t\tint s=0;\r\n\t\t\tfor(int i=0;i<find.length;i++){\r\n\t\t\t\tfor(int j=s;j<chars.length;j++){\r\n\t\t\t\t\tif(find[i] > chars[j]) continue;\r\n\t\t\t\t\tif(find[i] == chars[j]){\r\n\t\t\t\t\t\ts=j+1;\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t}\r\n\t\t\t\t\treturn false;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn true;\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic void load(String y){\r\n\t\tset = new ScrabbleSet(y.split(\" \"));\r\n\t}\r\n\r\n\tpublic static void main(String[] args) throws IOException {\r\n\t\targs = new String[]{\"Scrabble\\\\data\\\\english4000.dic\",\r\n\t\t\t\t\"g w x b d u t z q m o i e p o\"};\r\n\t\tFileReader fr =  new FileReader(args[0]);\r\n\t\tBufferedReader br = new BufferedReader(fr);\r\n\r\n\t\tRunner r = new Runner();\r\n\t\tr.load(args[1]);\r\n\t\tSystem.out.println(args[1]);\r\n\t\t\r\n\t\tString line;\r\n\t\twhile((line = br.readLine()) != null) {\r\n\t\t\tSystem.out.print(line+\" \");\r\n\t\t\tSystem.out.println(r.set.checkWord(line.toCharArray()));\r\n\t\t}\r\n\t\tbr.close();\r\n\t\t\r\n\t\t\r\n\t}\r\n\r\n}<\/pre>\n<p><\/code><\/p>\n<p>There&#8217;s probably a better way to do this, I am interested<br \/>\nto see what other solutions there are.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[28,75],"_links":{"self":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/888"}],"collection":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/comments?post=888"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/888\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}