500. Keyboard Row

500. Keyboard Row

class Solution {
	public static String[] findWords(String[] words) {
		String[] strs = { "QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM" };
		HashMap map = new HashMap();
		for (int i = 0; i < strs.length; i++) {
			for (char c : strs[i].toCharArray()) {
				map.put(c, i);// put  pair into the map
			}
		}
		ArrayList outArrList = new ArrayList();
		for (int j = 0; j < words.length; j++) {
			HashSet row = new HashSet();
			for (int i = 0; i < words[j].length(); i++) {
				Character current = words[j].charAt(i);
				row.add(map.get(Character.toUpperCase(current)));

			}
			if (row.size() == 1)
				outArrList.add(words[j]);
		}
		String[] out = new String[outArrList.size()];

		for (int i = 0; i < out.length; i++)
			out[i] = outArrList.get(i);
		return out;
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s