Personal Website and Computer Science TUR EN

Interview Question Longest Common Prefix

Last update: 12/27/2016 6:52:00 AM


The problem is to find a longest common prefix string in a string array. For example, when the input is {"fath", "fat", "fatih", "fati"}, the output should be 'fat'.

Problem #14 - Longest Common Prefix

Description: Write a function to find the longest common prefix string amongst an array of strings.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.LongestCommonPrefix;

 * @author fkabakci
 * Problem Description: Write a function to find the longest common prefix string amongst an array of strings.
 * Example:
 * Input: {"fath", "fat", "fatih", "fati"}
 * Output: fat
 * Solution:
 * 1. Get substring of the first string as a reference string which its indexes 0 to n (entire string).
 * 2. Compare with all of the others whether they have same prefix.
 * 3. If anything which does not start with the prefix in elements,
 * 	  	Start from beginning for comparison by setting loop variable i to 0 and then cut substring variable 1 (n).
 * 	  If something found, loop variable reaches the end and then it returns longest common prefix.
 * 4. If never caught up any common prefix, (n == 0) then return empty string.
public class LongestCommonPrefix {
	public String longestCommonPrefix(String[] strs) {
		if (strs == null || strs.length == 0)
			return "";

		int i = 1, n = strs[0].length();
		while (i < strs.length) {
			if (!strs[i].startsWith(strs[0].substring(0, n))) {
				i = 0;
				n = n - 1;
				if (n == 0)
					return "";
			i = i + 1;
		return strs[0].substring(0, n);

	public static void main(String[] args) {
		LongestCommonPrefix lcp = new LongestCommonPrefix();
		System.out.println(lcp.longestCommonPrefix(new String[] { "fath", "fat", "fatih", "fati" }));

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.