From 071d1bbc049de3a9c5c9a913443facd3426e31fe Mon Sep 17 00:00:00 2001 From: Alex Scrobot Date: Sun, 9 Apr 2023 15:32:15 +0200 Subject: [PATCH] feat: add "expand around center" approach visualization --- .../Expand Around Center/README.md | 13 +++++ .../Expand Around Center/code.js | 51 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 Dynamic Programming/Expand Around Center/README.md create mode 100644 Dynamic Programming/Expand Around Center/code.js diff --git a/Dynamic Programming/Expand Around Center/README.md b/Dynamic Programming/Expand Around Center/README.md new file mode 100644 index 00000000..b37e4100 --- /dev/null +++ b/Dynamic Programming/Expand Around Center/README.md @@ -0,0 +1,13 @@ +# Expand Around Center + +The "expand around center" approach is a common algorithmic technique used to solve problems related to palindromes or symmetric sequences. The basic idea is to consider each position in the sequence as a possible center of a palindrome and then expand outwards in both directions from that center to find the longest palindrome or the number of palindromic substrings. + +For example, consider the string "racecar". We can start by considering each character in the string as a possible center, and then expand around it to check if it forms a palindrome. If we start with the center at index 3 (the letter "e"), we can expand outwards in both directions to find that the longest palindrome centered at index 3 is "cec". Similarly, we can check all other centers to find the longest palindrome in the string. + +The "expand around center" approach is a simple and efficient way to solve many palindrome-related problems. It can be used to find the longest palindromic substring in a given string, count the number of palindromic substrings, or check if a given string is a palindrome. + +The "expand around center" approach is efficient, with a time complexity of O(n^2), where n is the length of the input string. + +## References + +- [@bhprtk/longest-palindromic-substring](https://medium.com/@bhprtk/longest-palindromic-substring-a8190fab03ff#:~:text=Expand%20Around%20Center%3A,the%20center%20is%20%22bb%22%20.) diff --git a/Dynamic Programming/Expand Around Center/code.js b/Dynamic Programming/Expand Around Center/code.js new file mode 100644 index 00000000..9ab6bf03 --- /dev/null +++ b/Dynamic Programming/Expand Around Center/code.js @@ -0,0 +1,51 @@ +const { Tracer, Array1DTracer, Layout, VerticalLayout } = require('algorithm-visualizer'); + +const tracer = new Array1DTracer('Longest Palindromic Substring'); +Layout.setRoot(new VerticalLayout([tracer])); + +function expandAroundCenter(s, left, right) { + tracer.select(left, right); + Tracer.delay(); + + while (left >= 0 && right < s.length && s[left] === s[right]) { + left--; + right++; + tracer.select(left, right); + Tracer.delay(); + } + + tracer.deselect(left, right); + return right - left - 1; +} + +function longestPalindromicSubstring(s) { + if (s.length === 0 || s.length === 1) { + return s; + } + + let start = 0; + let end = 0; + + tracer.set(s); + Tracer.delay(); + + for (let i = 0; i < s.length; i++) { + const len1 = expandAroundCenter(s, i, i); + const len2 = expandAroundCenter(s, i, i + 1); + const maxLen = Math.max(len1, len2); + + if (maxLen > end - start) { + start = i - Math.floor((maxLen - 1) / 2); + end = i + Math.floor(maxLen / 2); + } + } + + tracer.select(start, end); + Tracer.delay(); + tracer.deselect(start, end); + + return s.slice(start, end + 1); +} + +const input = 'babad'; +console.log(longestPalindromicSubstring(input)); // Output: "bab" or "aba"