1+ //comparing two substring and return max length of substring which is equal
2+
3+ package StringBasedProblems ;
4+
5+ public class SubstringEqual {
6+
7+ static String substring (char [] c ,int i ,int j ){
8+ String s ="" ;
9+ for (int k =i ;k <j ;k ++)
10+ s +=c [k ];
11+ return s ;
12+
13+ }
14+ public static void main (String [] args ) {
15+ String s2 ="string" ,s1 ="asdfestrderingl" ;
16+ char [] c1 =s1 .toCharArray (),c2 =s2 .toCharArray ();
17+ // int i=0,max=0;
18+ // while(i<c1.length-1){
19+ // int j=i+1;
20+ // while(j<=c2.length){
21+ // int flag=0,k=0;
22+ // String r1=substring(c1, i, j);
23+ // while(k<c2.length-1){
24+ // int l=k+1;
25+ // while(l<=c2.length){
26+ // String r2=substring(c2, k, l);
27+ // if(r1.equals(r2)){
28+ // int len=r1.length();
29+ // if(max<len)
30+ // max=len;
31+ // flag=1;
32+ // break;
33+ // }
34+ // l++;
35+ // }
36+ // if(flag==1)
37+ // break;
38+ // k++;
39+ // }
40+ // j++;
41+ // }
42+ // i++;
43+ // }
44+
45+ // System.out.println(max);
46+
47+
48+
49+
50+ int i =0 ,max =0 ;
51+ while (i <c1 .length ){
52+ int j =0 ,k =i ;
53+ while (j <c2 .length )
54+ {
55+ int count =0 ;
56+
57+ while ( j <c2 .length && i <c1 .length && c1 [k ]==c2 [j ]){
58+ System .out .println (c1 [k ]);
59+ k ++;
60+ j ++;
61+ count ++;
62+ if (j >=c2 .length || i >=c1 .length )
63+ break ;
64+ // flag=1;
65+ }
66+ k =i ;
67+ j ++;
68+ if (count >0 )
69+ System .out .println (count );
70+ if (max <count )
71+ max =count ;
72+ }
73+ i ++;
74+ // System.out.println(count);
75+
76+ }
77+ System .out .println (max );
78+
79+
80+ // int max=0;
81+ // for(int i=0;i<c1.length;i++){
82+ // int count=0;
83+ // for(int j=i;j<c2.length;j++){
84+ // if(c1[j]==c2[j])
85+ // count++;
86+ // }
87+ // if(count>max)
88+ // max=count;
89+ // }
90+ // System.out.println(max);
91+ }
92+ }
93+
94+
95+
96+
97+ //input
98+
99+ //s2="string",s1="asdfestrderingl";
100+
101+ //output
102+
103+
104+ //sub len
105+
106+ // s ->1
107+ // str ->3
108+ //tr ->2
109+ //r ->1
110+ //ring ->4
111+ //ing ->3
112+ //ng ->2
113+ //g ->1
114+
115+ // max=4
0 commit comments