Personal Website and Computer Science TUR EN

Interview Question Keyboard Row

Last update: 2/21/2017 8:08:00 PM


The problem is to find out which words can be typed by the characters which are in same row in an American keyboard.

Problem #500 - Keyboard Row

Description: Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.

Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]

1. You may use one character in the keyboard more than once.
2. You may assume the input string will only contain letters of alphabet.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.KeyboardRow;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

 * @author fkabakci 
 * Problem Description: Given a List of words, return the words that can be typed using letters of alphabet 
 * on only one row s of American keyboard like the image below.
 * Example 1:
 * Input: ["Hello", "Alaska", "Dad", "Peace"]
 * Output: ["Alaska", "Dad"]
 * Solution:
 * 1. Build a keyboard.
 * 2. Get a word, and see the first character what row number it belongs to.
 * 3. Then, check out the rest of characters if they have same row number,
 * 	  if so, add them into a list.
 * 4. Return it.
public class Solution {
	String[] keyboard = {"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"};
	private int getRowNumber(char letter) {
		for(int i = 0; i < keyboard.length; i++) {
			for(char keyboardLetter : keyboard[i].toCharArray()) {
				int asciiLetter = (int)letter;
				int asciiKeyboardLetter = (int)keyboardLetter;
				if(asciiLetter == asciiKeyboardLetter || asciiLetter == asciiKeyboardLetter + 32)
					return i;
		return -1;
	private boolean isOneRow(String word) {
		char[] words = word.toCharArray();
		int rowNumber = getRowNumber(words[0]);
		for(int i = 1; i < words.length; i++) {
			if(rowNumber != getRowNumber(words[i]))
				return false;
		return true;
	public String[] findWords(String[] words) {
		List<String> list = new ArrayList<String>();
		for(String word : words)
		return list.toArray(new String[]{});

	public static void main(String[] args) {
		Solution s = new Solution();
		System.out.println(Arrays.toString(s.findWords(new String[] {"Alaska", "Hello", "Dad"})));

There has been no comment yet


   Please verify the image

The Topics in Computer Science

Search this site for


Software & Algorithms


In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages


A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#



A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.



Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies


Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies


Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME



A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems


An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science


Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.