- Notifications
You must be signed in to change notification settings - Fork180
DRAFT: feat: add "expand around center" approach visualization#36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
scrobot wants to merge1 commit intoalgorithm-visualizer:masterChoose a base branch fromscrobot:expand_around_center
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletionsDynamic Programming/Expand Around Center/README.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.) |
51 changes: 51 additions & 0 deletionsDynamic Programming/Expand Around Center/code.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.