Longest Palindrome | 3 Easy Approaches | Leetcode 409 | codestorywithMIK

Subscribers:
92,300
Published on ● Video Link: https://www.youtube.com/watch?v=DSEng0xILV4



Duration: 19:16
5,165 views
251


Whatsapp Community Link : https://www.whatsapp.com/channel/0029...
This is the 40th Video of our Playlist "Leetcode Easy : Popular Interview Problems" by codestorywithMIK

In this video we will try to solve a good practice String problem : Longest Palindrome | 3 Easy Approaches | Leetcode 409 | codestorywithMIK

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Longest Palindrome | 3 Easy Approaches | Leetcode 409 | codestorywithMIK
Company Tags : will update soon
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Intervie...
Leetcode Link : https://leetcode.com/problems/longest...


My DP Concepts Playlist :    • Roadmap for DP | How to Start DP ? | ...  
My Graph Concepts Playlist :    • Graph Concepts & Qns - 1 : Graph will...  
My Recursion Concepts Playlist :    • Introduction | Recursion Concepts And...  
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Intervie...
Instagram :   / codestorywithmik  
Facebook :   / 100090524295846  
Twitter :   / cswithmik  
Subscribe to my channel :    / @codestorywithmik  

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Summary :
Approach 1: Using a Set

Time Complexity (T.C): O(n)
Space Complexity (S.C): O(n)
Method:
Initialize an empty set to track characters.
Traverse the string and for each character, check if it is already in the set.
If it is, remove it from the set and increment the result by 2.
If it is not, add it to the set.
If the set is not empty at the end, add 1 to the result to account for one central character.
Summary: This approach leverages a set to find pairs of characters. Each time a pair is found, it contributes 2 to the palindrome length. If there are any leftover characters, one can be used as the center of the palindrome.
Approach 2: Using a HashMap and Counting Frequency

Time Complexity (T.C): O(n)
Space Complexity (S.C): O(n)
Method:
Initialize a HashMap to count the frequency of each character in the string.
Traverse the HashMap to accumulate the length of characters that can be fully paired.
For each character count, add the even part to the result.
Track if there is any character with an odd count.
If there is a character with an odd count, add 1 to the result for the central character.
Summary: This approach uses a HashMap to count frequencies of characters. Even frequencies are directly added to the result, while odd frequencies contribute one less than their count to maintain pairs, with the possibility of using one odd character as the center.
Approach 3: One Iteration

Time Complexity (T.C): O(n)
Space Complexity (S.C): O(n)
Method:
Initialize a HashMap to count the frequency of each character and an odd frequency counter.
Traverse the string and update the frequency of each character in the HashMap.
Adjust the odd frequency counter based on whether the current frequency is odd or even.
Calculate the result based on the total length minus the number of odd frequencies plus one (if any odd frequency exists).
Summary: This approach combines counting character frequencies with tracking the number of characters that have odd frequencies in a single pass through the string. The result is calculated by subtracting the number of odd frequencies from the total length and adding one if any odd frequencies exist, ensuring the longest possible palindrome.
All three approaches efficiently calculate the length of the longest palindrome that can be formed from a given string, each with a time complexity of O(n) and space complexity of O(n). The choice between them depends on the preferred data structures and slight variations in implementation.


✨ Timelines✨
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024




Other Videos By codestorywithMIK


2024-06-13Minimum Increment to Make Array Unique | Sorting | Counting Sort | Leetcode 945 | codestorywithMIK
2024-06-13IPO | Easy Beginner Friendly | Story To Code | Leetcode 502 | codestorywithMIK
2024-06-12Minimum Number of Moves to Seat Everyone | Counting Sort | Sort | Leetcode 2037 | codestorywithMIK
2024-06-09Relative Sort Array | Counting Sort | Using Lambda | Leetcode 1122 | codestorywithMIK
2024-06-06How codestorywithMIK was started | Motivation | Rejections | 50K Special
2024-06-06Replace Words | Using TRIE | Uber | Leetcode 648 | codestorywithMIK
2024-06-05Replace Words | Using HashSet | Uber | Leetcode 648 | codestorywithMIK
2024-06-04Hand of Straights | Simple Thought Process | Google | Leetcode 846 | codestorywithMIK
2024-06-03Find Common Characters | Simple Dry Run | Leetcode 1002 | codestorywithMIK
2024-06-03Find Subarray With Bitwise AND Closest to K | Sliding Window | Leetcode 3171 | codestorywithMIK
2024-06-02Longest Palindrome | 3 Easy Approaches | Leetcode 409 | codestorywithMIK
2024-06-01Append Characters to String to Make Subsequence | Simple Approach | Leetcode 2486 | codestorywithMIK
2024-05-31Lexicographically Minimum String After Removing Stars | Easy | Leetcode 3170 | codestorywithMIK
2024-05-29Single Number III | Detailed Explanation | Leetcode 260 | codestorywithMIK
2024-05-28Count Triplets That Can Form Two Arrays of Equal XOR | Leetcode 1442 | codestorywithMIK
2024-05-27Number of Steps to Reduce a Number in Binary Representation to One | 2 Approaches | Leetcode 1404
2024-05-26Get Equal Substrings Within Budget | Classic Sliding Window | Leetcode 1208 | codestorywithMIK
2024-05-25Special Array With X Elements Greater Than or Equal X | 3 Approaches |Leetcode 1608|codestorywithMIK
2024-05-24Student Attendance Record II | Recursion | Memoization | Bottom Up | Leetcode 552 | codestorywithMIK
2024-05-07Relative Ranks | 3 Approaches | Leetcode 506 | codestorywithMIK
2024-05-07Double a Number Represented as a Linked List | 4 Approaches | Story|Leetcode 2816 | codestorywithMIK



Other Statistics

Counter-Strike: Source Statistics For codestorywithMIK

At this time, codestorywithMIK has 34,577 views for Counter-Strike: Source spread across 5 videos. Less than an hour worth of Counter-Strike: Source videos were uploaded to his channel, making up less than 0.10% of the total overall content on codestorywithMIK's YouTube channel.