-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion14.java
More file actions
64 lines (54 loc) · 1.09 KB
/
Copy pathQuestion14.java
File metadata and controls
64 lines (54 loc) · 1.09 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 14
*/
/*
TestCases-
*/
public class Question14
{
public static String longestCommonPrefix(String[] strs)
{
char ch;
int index = 0;
String temp="";
int min = Integer.MAX_VALUE;
for(int i = 0;i<strs.length;i++)
{
if(strs[i].length()<min)
{
min = strs[i].length();
temp = strs[i];
}
}
System.out.println(min);
if(min==0)
return "";
int j;
abcd:
for(j = 0;j<min;j++)
{
ch = temp.charAt(j);
for(int i = 0;i<strs.length;i++)
{
if(strs[i].charAt(j) != ch)
break abcd;
}
}
if (temp.substring(0,j).equals(""))
return "";
else
return temp.substring(0,j);
}
public static void main(String[] args)
{
System.out.println("Main Method starts");
String[] arr = {"Ramu","Shyamu","Bheemu"};
String[] arr1 = {"Ram","Rama","Ramachandra"};
System.out.println(arr.length);
arr.length = 5;
System.out.println(longestCommonPrefix(arr));
System.out.println(longestCommonPrefix(arr1));
}
}