Longest Pallindromic Substring

Duration: 26 min

This video lesson is available to enrolled students.

Return to /learn/TCS-NQT-PREPARATION/advanced-coding/dsa-using-python/strings/asset-longest-pallindromic-substring after enrolling

Inside: a video lesson and guided study material.

Module outline

  1. TCS One Shot Free Videos & Notes: TCS Preparation
  2. Numerical Ability (Foundation Section): Ratio and Proportion (Ratios), Divisibility Rules, Data Interpretation, Mixture and Alligation, Permutation and Combination, Logarithm, Number System, HCF LCM, Sequence and Series (Series), Speed Time and Distance, Percentage, Average, Powers and Exponents (Surds and Indices), Algebra, Probability, Profit and Loss, Simple Interest and Compound Interest, Time and Work, Pipe and Cistern, Mensuration and Geometry, Mental Ability, Age Problems, Statistics, Arithmetic, Partnership, Speed Mathematics and Basic Mathematics
  3. Reasoning Ability (Foundation Section): Series (Number and Letter Series) (Numerical Relations and Reasoning), Coding Decoding, Data Sufficiency, Non Verbal Reasoning (Spatial Aptitude) (Spatial Reasoning) (Visual Reasoning), Syllogisms, Deductive and Inductive Reasoning (Logical Deduction and Induction) (Prepositional Reasoning), Seating Arrangements, Blood Relations, Directions (Direction Test), Analytical Reasoning (Counting Figures Reasoning), Puzzle Solving (Puzzles), Calendar, Clock, Cubes & Dices, Selection Decision Table (Decision Making), Crypt Arithmetic, Venn Diagram, Data Arrangement, Inequality, Analogy, Ranking, Order and Sequence, Logical Sequence Of Words
  4. New Pattern-Verbal Ability (Foundation Section): Sentence Completion (Fill in the blanks), English Writing Ability
  5. Old Pattern-Verbal Ability (Foundation Section): Vocabulary, Noun, Subject Verb Agreement (Verb Noun Agreement), Adjectives, Tenses, Pronoun, Preposition, Articles, Direct and Indirect Speech, Conjunction, Modals, Sentence Re-arrangements (Para Jumbles) (Narrative Sequencing), Sentence Correction (Error Correction), Comprehension / Reading Comprehension / Unseen Passages (Critical Reasoning) (Paragraph Questions), Verbal Analogy (Word Based Analogy), Active Passive Voice (Voice), Determiner, Facts, Inference and Judgements, Adverb, Cloze Test (Cloze Passage), Sentence Construction, Verb
  6. Advanced Quantitative Ability: TCS NQT Advanced Quantitative Ability
  7. Advanced Reasoning Ability: TCS NQT Advanced Reasoning Ability
  8. Advanced Coding: Coding Questions by Yash Sir, C Programming by Yash Sir, Python for Placements by Yash Sir, DSA using Python, Object Oriented Programming (OOPS), Programming in C++, Java
  9. TCS Full Length Mock Test:
  10. TCS Interview & Resume Prep: MR & HR Interview Preparation, TCS Interview Preparation, Mock Interview, Interview Recordings, Resume Preparation, Resume Review
  11. Mega Test:
  12. Grand Test:
AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This video is a comprehensive tutorial on solving the 'Longest Palindromic Substring' problem, a common algorithmic challenge. The instructor begins by presenting the problem statement from a coding platform, explaining that the goal is to find the longest substring within a given string that reads the same forwards and backwards. The core of the lesson is the 'Expand Around Center' algorithm, which is explained both conceptually and through a step-by-step implementation. The instructor uses a digital whiteboard to illustrate the method, showing how to treat each character (and each gap between characters) as a potential center of a palindrome and expand outwards to find the longest possible palindrome. This is followed by a detailed walkthrough of the Python code, where the `expand_around_center` function is defined to handle both odd-length (e.g., 'aba') and even-length (e.g., 'abba') palindromes. The main function iterates through each possible center, calls the helper function, and keeps track of the longest palindrome found. The video concludes with the instructor writing the complete, working code in an online IDE, demonstrating its execution and showing the accepted solution with performance metrics.

Chapters

  1. 0:00 – 2:00 00:00-02:00

    The video opens on a coding platform, leetcode.com, displaying the problem '5. Longest Palindromic Substring'. The problem description, examples, and constraints are visible. The instructor explains the task: given a string, return the longest palindromic substring. The code editor on the right shows a skeleton of a Python class with a method `longestPalindrome` that takes a string `s` and returns a string. The instructor then transitions to a digital whiteboard to begin the explanation.

  2. 2:00 – 5:00 02:00-05:00

    On the digital whiteboard, the instructor writes the problem title '26 Longest Palindromic Substring'. They introduce the core concept: 'Expand around centre'. Using the example string 'babad', they demonstrate how to treat each character as a potential center of a palindrome and expand outwards. They draw diagrams showing the expansion for the center at 'b' (resulting in 'bab') and at 'a' (resulting in 'aba'). They also illustrate the concept of even-length palindromes by showing the center between 'b' and 'a' in 'baab'.

  3. 5:00 – 10:00 05:00-10:00

    The instructor continues on the whiteboard, explaining the algorithm in more detail. They write the pseudocode for the main function `longestPalindrome(s)` and the helper function `expand_around_center(left, right)`. They emphasize that the algorithm must handle both odd and even length palindromes by calling the helper function twice for each center: once with `i` and `i` (for odd), and once with `i` and `i+1` (for even). They write the logic for the `expand_around_center` function, which uses a while loop to expand as long as the characters at the left and right pointers are equal and within bounds.

  4. 10:00 – 15:00 10:00-15:00

    The instructor completes the pseudocode on the whiteboard. They write the `expand_around_center` function, which returns the substring from `left` to `right` (inclusive). They then write the main loop of `longestPalindrome`, which iterates through each index `i` in the string. For each `i`, it calls `expand_around_center(i, i)` and `expand_around_center(i, i+1)`, compares the lengths of the resulting palindromes, and updates the `longest` variable if a longer one is found. They also write the base case for a single character string.

  5. 15:00 – 20:00 15:00-20:00

    The instructor transitions back to the coding environment. They begin to write the actual Python code based on the whiteboard pseudocode. They define the `expand_around_center` function, which takes `left` and `right` indices and returns the substring. They implement the while loop to expand the palindrome, checking bounds and character equality. They then define the `longestPalindrome` function, initialize `longest` to an empty string, and loop through the string to find the longest palindrome by calling the helper function for both odd and even cases.

  6. 20:00 – 25:00 20:00-25:00

    The instructor completes the code in the IDE. They write the logic to compare the lengths of the two palindromes found at each center and update the `longest` variable accordingly. They add a final return statement for the `longestPalindrome` function. The complete code is shown, and the instructor runs it, which results in an 'Accepted' status with a runtime of 276 ms and memory usage of 16.70 MB.

  7. 25:00 – 26:13 25:00-26:13

    The video concludes with a final view of the accepted solution in the online IDE. The code is fully written and has passed all test cases. The instructor's final comment is to 'submit' the solution. The screen shows the accepted status, the runtime, and memory usage, confirming the successful implementation of the 'Expand Around Center' algorithm.

The video provides a clear, step-by-step guide to solving the Longest Palindromic Substring problem. It effectively combines a conceptual explanation using a whiteboard to illustrate the 'Expand Around Center' method with a practical implementation in Python. The instructor methodically breaks down the problem, first explaining the core idea of treating each character as a center, then detailing the code structure, and finally showing the complete, working solution. This approach ensures a deep understanding of both the algorithm's logic and its practical application, making it an excellent resource for learning this classic algorithmic challenge.

Loading lesson…