Skip to content

Commit

Permalink
Added Longest Palindromic Substring [JavaScript] (iiitv#412)
Browse files Browse the repository at this point in the history
* Added LongestPalindromicSubsequence [JavaScript]

* Added LongestPalindromicSubsequence [JavaScript]

* Added LongestPalindromicSubsequence [JavaScript]

* Bot fix v1.0

* Bot fix v1.1

* Splitting some lines :O

* Bleh
  • Loading branch information
anshumanv authored and aviaryan committed Aug 9, 2017
1 parent e926373 commit acbf7f1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [Linear Regression](https://en.wikipedia.org/wiki/Linear_regression) | | | | [:white_check_mark:](linear_regression/linear_regression.py) | | |
| [Linear Search](https://en.wikipedia.org/wiki/Linear_search) | [:white_check_mark:](linear_search/linear_search.c) | | [:white_check_mark:](linear_search/LinearSearch.java) | [:white_check_mark:](linear_search/linear_search.py) | [:white_check_mark:](linear_search/linear-search.go) | [:white_check_mark:](linear_search/linearSearch.js) |
| [Longest Common Subsequence](http://www.geeksforgeeks.org/dynamic-programming-set-4-longest-common-subsequence) | [:white_check_mark:](longest_common_subsequence/longestCommonSubsequence.c) | | [:white_check_mark:](longest_common_subsequence/LongestCommonSubsequence.java) | [:white_check_mark:](longest_common_subsequence/longest_common_subsequence.py) | [:white_check_mark:](longest_common_subsequence/longestCommonSubsequence.go) | [:white_check_mark:](longest_common_subsequence/longestCommonSubsequence.js) |
| [Longest Palindromic Substring](http://www.geeksforgeeks.org/longest-palindrome-substring-set-1/) | | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.cpp) | [:white_check_mark:](longest_palindromic_substring/LongestPalindromicSubstring.java) | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.py) | | |
| [Longest Palindromic Substring](http://www.geeksforgeeks.org/longest-palindrome-substring-set-1/) | | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.cpp) | [:white_check_mark:](longest_palindromic_substring/LongestPalindromicSubstring.java) | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.py) | | [:white_check_mark:](longest_palindromic_substring/longestPalindromicSubstring.js) |
| [Merge Sort](https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/overview-of-merge-sort) | [:white_check_mark:](merge_sort/merge_sort.c) | | [:white_check_mark:](merge_sort/MergeSort.java) | [:white_check_mark:](merge_sort/merge_sort.py) | [:white_check_mark:](merge_sort/merge_sort.go) | [:white_check_mark:](merge_sort/mergeSort.js) |
| [Modular Exponential](http://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/) | [:white_check_mark:](modular_exponential/modular_exponential.c) | | [:white_check_mark:](modular_exponential/ModularExponential.java) | [:white_check_mark:](modular_exponential/modular_exponential.py) | [:white_check_mark:](modular_exponential/modular_exponential.go) | [:white_check_mark:](modular_exponential/modularExponential.js) |
| [N-Queen Problem](https://en.wikipedia.org/wiki/Eight_queens_puzzle) | | [:white_check_mark:](n_queen_problem/NQueenProblem.cpp) | [:white_check_mark:](n_queen_problem/NQueenProblem.java) | [:white_check_mark:](n_queen_problem/n_queen_problem.py) | | |
Expand Down
46 changes: 46 additions & 0 deletions longest_palindromic_substring/longestPalindromicSubstring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function expandAroundCenter (test, left, right) {
/*
:param test: Input string whose substrings are to be checked
:param left: Left end of result palindromic substring
:param right: Right end of result palindromic substring
:return: Length of palindromic substring
*/
let n = test.length;
while (left >= 0 && right < n && test.charAt(left) === test.charAt(right)) {
left--;
right++;
}
return right - left - 1;
}

function longestPalindromicSubstring (test) {
/*
Function to find longest substring which is a palindrome
:param test: Input string whose substrings are to be checked
:return: Longest substring of input string which is a palindrome
Time complexity: O(n^2)
Space complexity: O(1)
*/
let start = 0;
let end = 0;

for (let i = 0; i < test.length; i++) {
let length = Math.max(expandAroundCenter(test, i, i),
expandAroundCenter(test, i, i + 1));
if (length > end - start) {
start = i - Math.floor((length - 1) / 2);
end = i + Math.floor(length / 2);
}
}
return test.slice(start, end + 1);
}

function main () {
let test = 'geeksforgeeks';
console.log('Longest Palindromic Substring of',
test, 'is', longestPalindromicSubstring(test));
}

main();

0 comments on commit acbf7f1

Please sign in to comment.