ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Longest Palindromic Substring

PHP

Goal -- WPM

Ready
Exercise Algorithm Area
1<?php
2
3function longestPalindrome(string $s): string
4{
5$n = strlen($s);
6if ($n < 2) {
7return $s;
8}
9
10// dp[i][j] will be true if the string from index i to j is a palindrome.
11$dp = array_fill(0, $n, array_fill(0, $n, false));
12$start = 0;
13$maxLength = 1;
14
15// All substrings of length 1 are palindromes.
16for ($i = 0; $i < $n; $i++) {
17$dp[$i][$i] = true;
18}
19
20// Check for substrings of length 2.
21for ($i = 0; $i < $n - 1; $i++) {
22if ($s[$i] === $s[$i + 1]) {
23$dp[$i][$i + 1] = true;
24$start = $i;
25$maxLength = 2;
26}
27}
28
29// Check for lengths greater than 2. k is the length of the substring.
30for ($k = 3; $k <= $n; $k++) {
31// Fix the starting index.
32for ($i = 0; $i <= $n - $k; $i++) {
33// Get the ending index of the substring from starting index i and length k.
34$j = $i + $k - 1;
35
36// Check if the inner substring is a palindrome and if the outer characters match.
37if ($dp[$i + 1][$j - 1] && $s[$i] === $s[$j]) {
38$dp[$i][$j] = true;
39
40if ($k > $maxLength) {
41$start = $i;
42$maxLength = $k;
43}
44}
45}
46}
47
48return substr($s, $start, $maxLength);
49}
50
51?>
Algorithm description viewbox

Longest Palindromic Substring

Algorithm description:

This function finds the longest palindromic substring within a given string. It utilizes dynamic programming to efficiently determine all palindromic substrings. A common use case is in text processing or bioinformatics where identifying symmetrical patterns is crucial.

Algorithm explanation:

The algorithm uses a 2D boolean array `dp` where `dp[i][j]` is true if the substring from index `i` to `j` (inclusive) is a palindrome. It initializes `dp` for single characters and then iteratively builds up solutions for longer substrings. The time complexity is O(n^2) because we fill an n x n DP table. The space complexity is also O(n^2) for the DP table. Edge cases like empty strings or strings with a single character are handled. The correctness relies on the fact that a substring is a palindrome if its inner substring is a palindrome and its outer characters match.

Pseudocode:

function longestPalindrome(s):
  n = length(s)
  if n < 2: return s

  dp = 2D array of size n x n, initialized to false
  start = 0, maxLength = 1

  // Base case: single characters are palindromes
  for i from 0 to n-1:
    dp[i][i] = true

  // Base case: two characters
  for i from 0 to n-2:
    if s[i] == s[i+1]:
      dp[i][i+1] = true
      start = i
      maxLength = 2

  // Check for lengths > 2
  for k from 3 to n:
    for i from 0 to n-k:
      j = i + k - 1
      if dp[i+1][j-1] and s[i] == s[j]:
        dp[i][j] = true
        if k > maxLength:
          start = i
          maxLength = k

  return substring of s from start with length maxLength