FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java_Programs/CommonSubstring.java at main · prittoruban/Java_Programs · GitHub
prittoruban
/
Java_Programs
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java_Programs
/
CommonSubstring.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
76 lines (68 loc) · 2.22 KB
Breadcrumbs
Java_Programs
/
CommonSubstring.java
Copy path
File metadata and controls
76 lines (68 loc) · 2.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Common Substring - Three String Values
* The program must accept three string values S1, S2 and S3 as the input.
* The program must print the longest common substring in S1, S2 and S3.
* If there are two or more longest common substring values, the program must print the first occurring longest common substring.
* If there is no such longest common substring, the program must print -1 as the output.
*
* Boundary Condition(s):
* 2 <= Length of S1, S2, S3 <= 100
*
* Input Format:
* The first line contains S1, S2 and S3 separated by a space.
*
* Output Format:
* The first line contains the longest common substring in S1, S2 and S3 or -1.
*
* Example Input/Output 1:
* Input:
* Abcdefghxyz xyz Abcxyz
* Output:
* xyz
* Explanation:
* The longest common substring in Abcdefghxyz, xyz and Abcxyz is xyz.
* So xyz is printed as the output.
*
* Example Input/Output 2:
* Input:
* Pqrstuv Ab@cdefgh ijklmn@
* Output:
* -1
*
* Max Execution Time Limit: 500 millisecs
* */
import
java
.
util
.*;
class
LongestCommonSubstring
{
public
static
void
main
(
String
[]
args
) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
String
S1
=
sc
.
next
();
String
S2
=
sc
.
next
();
String
S3
=
sc
.
next
();
int
len1
=
S1
.
length
();
int
len2
=
S2
.
length
();
int
len3
=
S3
.
length
();
int
[][][]
dp
=
new
int
[
len1
+
1
][
len2
+
1
][
len3
+
1
];
int
maxLength
=
0
;
int
endIndex
= -
1
;
for
(
int
i
=
1
;
i
<=
len1
;
i
++) {
for
(
int
j
=
1
;
j
<=
len2
;
j
++) {
for
(
int
k
=
1
;
k
<=
len3
;
k
++) {
if
(
S1
.
charAt
(
i
-
1
) ==
S2
.
charAt
(
j
-
1
) &&
S1
.
charAt
(
i
-
1
) ==
S3
.
charAt
(
k
-
1
)) {
dp
[
i
][
j
][
k
] =
dp
[
i
-
1
][
j
-
1
][
k
-
1
] +
1
;
if
(
dp
[
i
][
j
][
k
] >
maxLength
) {
maxLength
=
dp
[
i
][
j
][
k
];
endIndex
=
i
-
1
;
}
}
else
{
dp
[
i
][
j
][
k
] =
0
;
}
}
}
}
if
(
maxLength
==
0
) {
System
.
out
.
println
(-
1
);
}
else
{
System
.
out
.
println
(
S1
.
substring
(
endIndex
-
maxLength
+
1
,
endIndex
+
1
));
}
}
}
Back
|
FazBrowse Home
|
New Git URL