Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:master
base:master
Choose a base branch
Loading
fromscrobot:expand_around_center
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletionsDynamic Programming/Expand Around Center/README.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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"

[8]ページ先頭

©2009-2025 Movatter.jp