-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prefix.java
41 lines (41 loc) · 924 Bytes
/
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
class Prefix {
public static void longestCommonPrefix() {
String strs[]={"cir","car"};
int i, j, f;
int l=0;
int min=strs[0].length();
String r="", s="";
for(i=0; i<strs.length; i++)
{
l=strs[i].length();
if(l<=min)
{
min=l;
s=strs[i];
}
}
System.out.println(s);
char ch='1';
for(i=0; i<min; i++)
{
f=0;
try{
ch=s.charAt(i);
}
catch(Exception e)
{
System.out.println("");
}
for(j=0; j<strs.length; j++)
{
if(ch==strs[j].charAt(i))
f++;
}
if(f==strs.length)
r=r+ch;
else
break;
}
System.out.println(r);
}
}