Find the length of the longest palindromic subsequence in a given sequence

Complexity

  • Time:
  • Space:

References

Implementations

  • 1
    
    # TODO: Implement in Python
    
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    
    const seq = 'BBABCBCAB';
    let N;
    N = seq.length;
    
    const L = new Array(N);
    
    let i;
    let j;
    for (i = 0; i < N; i++) {
      L[i] = new Array(N);
    }
    for (i = 0; i < N; i++) {
      L[i][i] = 1;
    }
    
    function max(a, b) {
      if (a > b) {
        return a;
      }
      return b;
    }
    
    console.log('LPS for any string with length = 1 is 1');
    
    for (i = 2; i <= N; i++) {
      
      console.log('--------------------------------------------------');
      console.log(`Considering a sub-string of length ${i}`);
      console.log('--------------------------------------------------');
      
      for (j = 0; j < N - i + 1; j++) {
        const k = j + i - 1;
    
        console.log(`Comparing ${seq[j]} and ${seq[k]}`);
    
        if (seq[j] === seq[k] && i === 2) {
          
          console.log(`They are equal and size of the string in the interval${j} to ${k} is 2, so the Longest Palindromic Subsequence in the Given range is 2`);
    
          L[j][k] = 2;
          
        } else if (seq[j] === seq[k]) {
          
          console.log(`They are equal, so the Longest Palindromic Subsequence in the Given range is 2 + the Longest Increasing Subsequence between the indices ${j + 1} to ${k - 1}`);
    
          L[j][k] = L[j + 1][k - 1] + 2;
          
        } else {
          
          console.log(`They are NOT equal, so the Longest Palindromic Subsequence in the Given range is the maximum Longest Increasing Subsequence between the indices ${j + 1} to ${k} and ${j} to ${k - 1}`);
    
          L[j][k] = max(L[j + 1][k], L[j][k - 1]);
          
        }
        
        console.log('--------------------------------------------------');
    
      }
    }
    
    console.log(`Longest Increasing Subsequence of the given string = L[0][${N - 1}]=${L[0][N - 1]}`);