Last active
February 15, 2022 19:02
-
-
Save kiliczsh/5dbf16a84dc4946c107272a02db228dd to your computer and use it in GitHub Desktop.
5. Longest Palindromic Substring - https://leetcode.com/problems/longest-palindromic-substring/submissions/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var longestPalindrome = function(str) { | |
| let strLen = str.length; | |
| let maxLen = 0, leftResult = 0, rightResult = 0, index = 0; | |
| while(index < strLen){ | |
| let left = index-1; | |
| while(left >= 0 && str[index] == str[left]){ | |
| left--; | |
| } | |
| let right = index+1; | |
| while(right < strLen && str[index] == str[right]){ | |
| right++; | |
| } | |
| let end = right; | |
| while(left >= 0 && right < strLen && str[left] == str[right]){ | |
| left--; | |
| right++; | |
| } | |
| if(maxLen < right-left-1){ | |
| maxLen = right-left-1 | |
| leftResult = left+1; | |
| rightResult = right-1; | |
| } | |
| index = end | |
| } | |
| return str.substring(leftResult,rightResult+1); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment