Bernard Wong
  • Software Engineer
  • Problems
    • Lowest Common Ancestor of a Binary Tree
    • Longest Substring Without Repeating Characters
    • Longest Palindromic Substring
    • Longest Common Prefix
    • Isomorphic Strings
    • Integer to Roman
    • Frog Jump
    • Find the Difference
    • Find k closest elements to a given value
    • Longest Common Subsequence
    • Binary Search Tree from Sorted Array
    • Balanced Binary Tree
    • Sort Using Two Stacks
    • O(1) Stack
    • k-th element to last of a LinkedList
    • Dedup LinkedList
    • Check Rotated String
    • Compress String by Character Count
    • Escape HTML whitespace
    • Check String Permutation
    • Unique String
    • Container With Most Water
    • 4 Sum
    • 3 Sum Closest
    • 3 Sum
    • 2 Sum
    • Maximum Subarray
    • Nested List Weight Sum
    • Palindrome Number
    • Pow(x, n)
    • Regular Expression Matching
    • Remove Nth Node From End of List
    • Reverse Integer
    • Roman to Integer
    • Rotate Array
    • Search a 2D Matrix
    • Shortest Word Distance
    • Two Sum III - Data structure design
    • Valid Parentheses
    • ZigZag Conversion
    • Quicksort
    • Add Two Numbers
    • Best Time to Buy and Sell Stock
    • Letter Combinations of a Phone Number
  • Data Structures
    • Heap
Powered by GitBook
On this page
  1. Problems

Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S.

You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

import scala.annotation.tailrec

/**
  * find longest palindrome (length only)
  *
  * @param s
  * @return
  */
def longestPalindromeLength(s: String): Int = {
  @tailrec def check(prev: Int, next: Int, acc: Int): Int = {
    if (prev < 0 || next >= s.length || s(prev) != s(next)) acc
    else check(prev - 1, next + 1, acc + 2)
  }
  var maxLength = 0
  for (i <- 0 until s.length) {
    // check odd length and even length
    maxLength = maxLength max check(i, i, -1) max check(i, i + 1, 0)
  }
  maxLength
}

longestPalindromeLength("")
longestPalindromeLength("a")
longestPalindromeLength("bab")
longestPalindromeLength("baab")

/**
  * find longest palindrome (substring)
  *
  * @param s
  * @return
  */
def longestPalindrome(s: String): String = {
  @tailrec def check(prev: Int, next: Int, acc: Int): Int = {
    if (prev < 0 || next >= s.length || s(prev) != s(next)) acc
    else check(prev - 1, next + 1, acc + 2)
  }
  var maxLength, length, index = 0
  for (i <- 0 until s.length) {
    length = check(i, i, -1) max check(i, i + 1, 0)
    maxLength = maxLength max length
    if (length == maxLength) index = i
  }
  // recover string from index and length
  val diff = maxLength / 2
  maxLength match {
    case l if l % 2 == 0 => s.subSequence(index - diff + 1, index + diff + 1).toString
    case _ => s.subSequence(index - diff, index + diff + 1).toString
  }
}

longestPalindrome("bananas")
longestPalindrome("abracadabra")

PreviousLongest Substring Without Repeating CharactersNextLongest Common Prefix

Last updated 6 years ago