FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/DynamicProgramming/LongestPalindromicSubstring.java at master · AmaranthLIS/Java · GitHub
AmaranthLIS
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
DynamicProgramming
/
LongestPalindromicSubstring.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (47 loc) · 1.14 KB
Breadcrumbs
Java
/
DynamicProgramming
/
LongestPalindromicSubstring.java
Copy path
File metadata and controls
56 lines (47 loc) · 1.14 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
package
test
;
import
java
.
io
.*;
import
java
.
util
.*;
/*
* Algorithm explanation https://leetcode.com/problems/longest-palindromic-substring/
*/
public
class
LongestPalindromicSubstring
{
public
static
void
main
(
String
[]
args
) {
String
a
=
"babad"
;
String
b
=
"cbbd"
;
String
aLPS
=
LPS
(
a
);
String
bLPS
=
LPS
(
b
);
System
.
out
.
println
(
a
+
" => "
+
aLPS
);
System
.
out
.
println
(
b
+
" => "
+
bLPS
);
}
private
static
String
LPS
(
String
input
) {
if
(
input
==
null
||
input
.
length
() ==
0
){
return
input
;
}
boolean
arr
[][] =
new
boolean
[
input
.
length
()][
input
.
length
()];
int
start
=
0
,
end
=
0
;
for
(
int
g
=
0
;
g
<
input
.
length
();
g
++) {
for
(
int
i
=
0
,
j
=
g
;
j
<
input
.
length
();
i
++,
j
++) {
if
(
g
==
0
) {
arr
[
i
][
j
] =
true
;
}
else
if
(
g
==
1
) {
if
(
input
.
charAt
(
i
) ==
input
.
charAt
(
j
)) {
arr
[
i
][
j
] =
true
;
}
else
{
arr
[
i
][
j
] =
false
;
}
}
else
{
if
(
input
.
charAt
(
i
) ==
input
.
charAt
(
j
) &&
arr
[
i
+
1
][
j
-
1
]) {
arr
[
i
][
j
] =
true
;
}
else
{
arr
[
i
][
j
] =
false
;
}
}
if
(
arr
[
i
][
j
]) {
start
=
i
;
end
=
j
;
}
}
}
return
input
.
substring
(
start
,
end
+
1
);
}
}
Back
|
FazBrowse Home
|
New Git URL