-
-
Save Glorfindel83/0e905a9d3567aabc75fe52f4e896d79a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stackexchange.puzzling; | |
import java.awt.Point; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class AdjacentLetters { | |
public static void main(String[] args) throws Exception { | |
String[] rows = new String[] { "qwertyuiop", "asdfghjkl", "zxcvbnm" }; | |
for (int i = 0; i < rows.length; i++) { | |
for (int j = 0; j < rows[i].length(); j++) { | |
locations.put(rows[i].charAt(j), new Point(j * 2 + i, i)); | |
} | |
} | |
System.out.println(isAdjacentIsogram("zsedcvg")); | |
BufferedReader reader = new BufferedReader(new FileReader("wordlist.txt")); | |
String line, longestMatch = ""; | |
while ((line = reader.readLine()) != null) { | |
if (line.length() < longestMatch.length()) | |
continue; | |
if (!isAdjacentIsogram(line)) | |
continue; | |
longestMatch = line; | |
System.out.println(line); | |
} | |
} | |
private static Map<Character, Point> locations = new HashMap<>(); | |
private static boolean isAdjacentIsogram(String word) { | |
boolean[] used = new boolean[26]; | |
Point currentPoint = null; | |
for (int i = 0; i < word.length(); i++) { | |
char c = Character.toLowerCase(word.charAt(i)); | |
if (used[c - 'a']) | |
return false; | |
used[c - 'a'] = true; | |
Point point = locations.get(c); | |
if (currentPoint != null | |
&& (Math.abs(currentPoint.x - point.x) > 2 || Math.abs(currentPoint.y - point.y) > 1)) | |
return false; | |
currentPoint = point; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment