Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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