Lngs Palindmi Subsing Ld Pgamming Sluins | Ld Pblm Sluins in ++, Java, & Pyhn []

LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Problems C++

Hello Programmers/Coders, Today we are going to share solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python. At Each Problem with Successful submission with all Test Cases Passed, you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for the Longest Palindromic Substring in C++, Java & Python-LeetCode problem. We are providing the correct and tested solutions to coding problems present on LeetCode. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

About LeetCode

LeetCode is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews. 

LeetCode is for software engineers who are looking to practice technical questions and advance their skills. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.

LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either EasyMedium, or Hard.

LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:

  • Mathematics/Basic Logical Based Questions
  • Arrays
  • Strings
  • Hash Table
  • Dynamic Programming
  • Stack & Queue
  • Trees & Graphs
  • Greedy Algorithms
  • Breadth-First Search
  • Depth-First Search
  • Sorting & Searching
  • BST (Binary Search Tree)
  • Database
  • Linked List
  • Recursion, etc.

Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.

Link for the ProblemLongest Palindromic Substring– LeetCode Problem

Longest Palindromic Substring– LeetCode Problem

Problem:

Given a string s, return the longest palindromic substring in s.

Example 1:

Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.

Example 2:

Input: s = "cbbd"
Output: "bb"

Constraints:

  • 1 <= s.length <= 1000
  • s consist of only digits and English letters.
Longest Palindromic Substring– LeetCode Solutions
class Solution {
 public:
  string longestPalindrome(string s) {
    // @ and $ signs are sentinels appended to each end to avoid bounds checking
    const string& t = join('@' + s + '$', '#');
    const int n = t.length();

    // t[i - maxExtends[i]..i) ==
    // t[i + 1..i + maxExtends[i]]
    vector maxExtends(n);
    int center = 0;

    for (int i = 1; i < n - 1; ++i) {
      const int rightBoundary = center + maxExtends[center];
      const int mirrorIndex = center - (i - center);
      maxExtends[i] =
          rightBoundary > i && min(rightBoundary - i, maxExtends[mirrorIndex]);

      // Attempt to expand palindrome centered at i
      while (t[i + 1 + maxExtends[i]] == t[i - 1 - maxExtends[i]])
        ++maxExtends[i];

      // If palindrome centered at i expand past rightBoundary,
      // adjust center based on expanded palindrome.
      if (i + maxExtends[i] > rightBoundary)
        center = i;
    }

    // Find the maxExtend and bestCenter
    int maxExtend = 0;
    int bestCenter = -1;

    for (int i = 0; i < n; ++i)
      if (maxExtends[i] > maxExtend) {
        maxExtend = maxExtends[i];
        bestCenter = i;
      }

    const int l = (bestCenter - maxExtend) / 2;
    const int r = (bestCenter + maxExtend) / 2;
    return s.substr(l, r - l);
  }

 private:
  string join(const string& s, char c) {
    string joined;
    for (int i = 0; i < s.length(); ++i) {
      joined += s[i];
      if (i != s.length() - 1)
        joined += c;
    }
    return joined;
  }
};
class Solution {
  public String longestPalindrome(String s) {
    final String t = join('@' + s + '$', '#');
    final int n = t.length();

    // t[i - maxExtends[i]..i) ==
    // t[i + 1..i + maxExtends[i]]
    int[] maxExtends = new int[n];
    int center = 0;

    for (int i = 1; i < n - 1; ++i) {
      final int rightBoundary = center + maxExtends[center];
      final int mirrorIndex = center - (i - center);
      maxExtends[i] =
          rightBoundary > i && Math.min(rightBoundary - i, maxExtends[mirrorIndex]) > 0 ? 1 : 0;

      // Attempt to expand palindrome centered at i
      while (t.charAt(i + 1 + maxExtends[i]) == t.charAt(i - 1 - maxExtends[i]))
        ++maxExtends[i];

      // If palindrome centered at i expand past rightBoundary,
      // adjust center based on expanded palindrome.
      if (i + maxExtends[i] > rightBoundary)
        center = i;
    }

    // Find the maxExtend and bestCenter
    int maxExtend = 0;
    int bestCenter = -1;

    for (int i = 0; i < n; ++i)
      if (maxExtends[i] > maxExtend) {
        maxExtend = maxExtends[i];
        bestCenter = i;
      }

    return s.substring((bestCenter - maxExtend) / 2, (bestCenter + maxExtend) / 2);
  }

  private String join(final String s, char c) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); ++i) {
      sb.append(s.charAt(i));
      if (i != s.length() - 1)
        sb.append(c);
    }
    return sb.toString();
  }
}
class Solution:
  def longestPalindrome(self, s: str) -> str:
    # @ and $ signs are sentinels appended to each end to avoid bounds checking
    t = '#'.join('@' + s + '$')
    n = len(t)

    # t[i - maxExtends[i]..i) ==
    # t[i + 1..i + maxExtends[i]]
    maxExtends = [0] * n
    center = 0

    for i in range(1, n - 1):
      rightBoundary = center + maxExtends[center]
      mirrorIndex = center - (i - center)
      maxExtends[i] = rightBoundary > i and 
          min(rightBoundary - i, maxExtends[mirrorIndex])

      # Attempt to expand palindrome centered at i
      while t[i + 1 + maxExtends[i]] == t[i - 1 - maxExtends[i]]:
        maxExtends[i] += 1

      # If palindrome centered at i expand past rightBoundary,
      # adjust center based on expanded palindrome.
      if i + maxExtends[i] > rightBoundary:
        center = i

    # Find the maxExtend and bestCenter
    maxExtend, bestCenter = max((extend, i)
                                for i, extend in enumerate(maxExtends))
    return s[(bestCenter - maxExtend) // 2:(bestCenter + maxExtend) // 2]

Leave a Comment

Scroll to Top