GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/longest-palindromic-substring at gh-pages · codingwhite/leetcode

Expand file tree

/

longest-palindromic-substring

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

longest-palindromic-substring

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
README.md
README.md
 
 
Solution.java
Solution.java
 
 
index.md
index.md
 
 

README.md

Outline

Leetcode Blog Version

Here is the Leetcode Blog Version. This is a classic problem. I write my first leetcode accepted version based on that.

My own words version

Brute force + isPalindromic will run out of time.

Recursive

  • length 1 substring: all are palindromic
  • length 2 substring: only if two char are same

What if length 3


length 3 = length 1 + two char

0 1 2 3 4
a b c b a
+   ^    
|   |
+---+    


0 1 2 4 4
a b c b a
  +   ^     
  |   |
  +---+    

  • length n = inner length n - 2 is palindromic AND (first char == last char)

Store length n into P[lenght n][start index]

  • P[1][3] = true means that the substring starts from index 3 and 1 char long is palindromic.
  • P[5][2] = true means that the substring starts from index 2 and 5 char long is NOT palindromic.

a matrix for abcba


  0 1 2 3 4
  a b c b a
1 1 1 1 1 1
2 0 0 0 0 -
3 0 1 0 - -
4 0 0 - - -
5 1 - - - -
^
l
e
n

With the matrix, we can fill up the martix by P[len][i] = P[len -2][i + 1] && S[i] == S[i + len -1].

Note: the length 0 is useless, so just using P[len - 1] for len.

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.