We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 58a1007 commit b7c4922Copy full SHA for b7c4922
longest-palindromic-substring/forest000014.java
@@ -0,0 +1,29 @@
1
+/*
2
+# Time Complexity: O(n^2)
3
+# Spcae Complexity: O(1)
4
+*/
5
+class Solution {
6
+ public String longestPalindrome(String s) {
7
+ String ans = "";
8
+ for (int i = 0; i < s.length(); i++) {
9
+ int l = i;
10
+ int r = i;
11
+
12
+ while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
13
+ if (r - l + 1 > ans.length()) ans = s.substring(l, r + 1);
14
+ l--;
15
+ r++;
16
+ }
17
18
+ l = i;
19
+ r = i + 1;
20
21
22
23
24
25
26
27
+ return ans;
28
29
+}
0 commit comments