-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCommon_Prefix.java
50 lines (45 loc) · 1.52 KB
/
Common_Prefix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*Write a program to find the longest common prefix among an array of strings. The longest common prefix is the string that
all strings in the array have in common as a prefix.
For example, given the array of strings ["flower", "flow", "flight"], the program should return the string "fl" as it is the
longest common prefix among all the strings.
Write a program that takes an array of strings as input and returns the longest common prefix among them. */
import java.util.*;
public class Common_Prefix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Number of words: ");
int n = sc.nextInt();sc.nextLine();
String[] arr = new String[n];
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextLine();
}
String ans = "";
int min = arr[0].length();
for (int i = 1; i < arr.length; i++) {
if(arr[i].length()<min)
{
min = arr[i].length();
}
}
for (int i = 0; i < min; i++) {
boolean check = true;
for (int j = 1; j < arr.length; j++) {
if(arr[j-1].charAt(i) != arr[j].charAt(i))
{
check = false;
break;
}
}
if(check == true)
{
ans += arr[0].charAt(i);
}
else
{
break;
}
}
System.out.println(ans);
}
}