Construct K Palindrome Strings - Leetcode 1400 - Python
Skills:
Python for Data80%
Key Takeaways
The Construct K Palindrome Strings problem from LeetCode is solved using a Python algorithm that checks if it's possible to create exactly K palindrome strings from a given character set. The solution involves counting the occurrences of each character and checking if the number of characters with odd counts is less than or equal to K.
Full Transcript
hey everyone welcome back and let's write some more neat code today so today let's solve the problem construct K palindrome strings we're given a string s and an integer K and we want to create exactly K strings that are palindromes using this as our character set so basically we want to know if it's possible if it is we would return true if it's not we would return false so one thing's for sure we need at least K character in this character set imagine if we were just given a string of like just the character a could we create two different palr strings no because we don't even have two different characters so one thing we can easily check is just that the number of characters is greater than or equal to K after that it becomes more interesting so imagine this is a string that we're given and K is equal to 2 is it possible for us to create two strings that are PAL dromes well yes and one of the ways would be this string just by itself is a palindrome and with these remaining characters we can create a palindrome we could I think do e l and then put the b in the middle and then have L and then e this is a pal Drome obviously because this is a pair of characters that is the same this pair is also the same and then we have a character in the middle so how can we do this algorithmically though do we actually have to consider every single possible palindrome that we could create from this character set cuz that be very very inefficient to code up and probably be difficult to code up anyway so the main thing to notice is when it comes to paland dromes we need to have an even amount of occurrences of every single character because there needs to be a pair except for a single character which would be the middle character and pound drums don't have to have middle characters we could have like a pound Drome that's of even length then we could just create pal dromes like that so that's kind of the main observation that you need to make because once you make that observation you realize that we just need to get the count of every character in the input and we're not looking for specifically the characters that just show up once because like I said B in this case is a character that just shows up once and we can only have one like copy of a character like that but we actually could have had an odd number of bees that's not one we could have had three bees and that would have looked like this so it's not that we're looking for characters that just show up a single amount of time so B shows up a single amount of time but if it showed up an odd number of times for example three times that is also something we would be interested in so uh in this example we see a a shows up twice N shows up twice a b shows up a single time e shows up twice and I think L all show shows up twice so we see that we only have a single character that shows up an odd number of times so this means that no matter what we'd need at least one uh string that would be a poome for this character but then we could merge all the other characters into that same string or we could create separate strings if we wanted to basically since right now our odd count was one one character that showed up an odd number of times and that number is less than or equal to K which is two in this case we know it is possible for us to create two pamic strings for this example Elite code we see L shows up a single time e shows up three times T shows up a single time C shows up a single time and so now we could probably stop here we already see there's four characters that show up an odd number of times which is already greater than K so we need at least four different strings to kind of separate all of these characters out into pal dromic substrings so it would not be possible to create exactly three pandrum strings so we would return false in this case so this will be a linear time solution you could say it's linear space since we're dealing with lower case A through Z I guess you could also say it's constant space but anyways let's code it up now so the first thing I'm going to do is just check if K is greater than the number of characters that were given if that's the case we would have to return false it would wouldn't be possible to create K Different Strings anyway otherwise we're going to in Python at least it's really easy we can call counter on the string this will count the occurrences of every single character then among those counts I just want to know how many of them were odds so that's what this variable is going to represent I'm going to go through every count in that hashmaps values and then if it's odd so if this moded by two was equal to one then I would want to increment odd one way to do that is actually just to take this and then move that there so if it's odd this will be one and then one will be added to this otherwise it'll be zero and then this won't change and then after all of that we can just check if odd is less than or equal to k then we would return true so we can just return whatever that evaluates to so this is the whole code let's run it you can see here it works and it's pretty efficient if you found this helpful check out n code. for a lot more thanks for watching and I'll see you soon
Original Description
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews
🧑💼 LinkedIn: https://www.linkedin.com/in/navdeep-singh-3aaa14161/
🐦 Twitter: https://twitter.com/neetcode1
⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf
Problem Link: https://leetcode.com/problems/construct-k-palindrome-strings/description/
0:00 - Read the problem
0:30 - Drawing Explanation
4:21 - Coding Explanation
leetcode 1400
#neetcode #leetcode #python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeetCodeIO · NeetCodeIO · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Leetcode 149 - Maximum Points on a Line - Python
NeetCodeIO
Design Linked List - Leetcode 707 - Python
NeetCodeIO
Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
NeetCodeIO
Design Browser History - Leetcode 1472 - Python
NeetCodeIO
Number of Good Paths - Leetcode 2421 - Python
NeetCodeIO
Flip String to Monotone Increasing - Leetcode 926 - Python
NeetCodeIO
Maximum Sum Circular Subarray - Leetcode 918 - Python
NeetCodeIO
Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
NeetCodeIO
Concatenated Words - Leetcode 472 - Python
NeetCodeIO
Data Stream as Disjoint Intervals - Leetcode 352 - Python
NeetCodeIO
LFU Cache - Leetcode 460 - Python
NeetCodeIO
N-th Tribonacci Number - Leetcode 1137
NeetCodeIO
Best Team with no Conflicts - Leetcode 1626 - Python
NeetCodeIO
Greatest Common Divisor of Strings - Leetcode 1071 - Python
NeetCodeIO
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
NeetCodeIO
Insert into a Binary Search Tree - Leetcode 701 - Python
NeetCodeIO
Delete Node in a BST - Leetcode 450 - Python
NeetCodeIO
Shuffle the Array (Constant Space) - Leetcode 1470 - Python
NeetCodeIO
Fruits into Basket - Leetcode 904 - Python
NeetCodeIO
Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
NeetCodeIO
Naming a Company - Leetcode 2306 - Python
NeetCodeIO
As Far from Land as Possible - Leetcode 1162 - Python
NeetCodeIO
Shortest Path with Alternating Colors - Leetcode 1129 - Python
NeetCodeIO
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
NeetCodeIO
Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
NeetCodeIO
Contains Duplicate II - Leetcode 219 - Python
NeetCodeIO
Path with Maximum Probability - Leetcode 1514 - Python
NeetCodeIO
Add to Array-Form of Integer - Leetcode 989 - Python
NeetCodeIO
Unique Paths II - Leetcode 63 - Python
NeetCodeIO
Minimum Distance between BST Nodes - Leetcode 783 - Python
NeetCodeIO
Design Hashmap - Leetcode 706 - Python
NeetCodeIO
Range Sum Query Immutable - Leetcode 303 - Python
NeetCodeIO
Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
NeetCodeIO
Middle of the Linked List - Leetcode 876 - Python
NeetCodeIO
Course Schedule IV - Leetcode 1462 - Python
NeetCodeIO
Single Element in a Sorted Array - Leetcode 540 - Python
NeetCodeIO
Capacity to Ship Packages - Leetcode 1011 - Python
NeetCodeIO
IPO - Leetcode 502 - Python
NeetCodeIO
Minimize Deviation in Array - Leetcode 1675 - Python
NeetCodeIO
Longest Turbulent Array - Leetcode 978 - Python
NeetCodeIO
Last Stone Weight II - Leetcode 1049 - Python
NeetCodeIO
Construct Quad Tree - Leetcode 427 - Python
NeetCodeIO
Find Duplicate Subtrees - Leetcode 652 - Python
NeetCodeIO
Sort an Array - Leetcode 912 - Python
NeetCodeIO
Ones and Zeroes - Leetcode 474 - Python
NeetCodeIO
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
NeetCodeIO
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
NeetCodeIO
Concatenation of Array - Leetcode 1929 - Python
NeetCodeIO
Symmetric Tree - Leetcode 101 - Python
NeetCodeIO
Check Completeness of a Binary Tree - Leetcode 958 - Python
NeetCodeIO
Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
NeetCodeIO
Find Peak Element - Leetcode 162 - Python
NeetCodeIO
Accounts Merge - Leetcode 721 - Python
NeetCodeIO
Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
NeetCodeIO
Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
NeetCodeIO
Number of Zero-Filled Subarrays - Leetcode 2348 - Python
NeetCodeIO
Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
NeetCodeIO
Sqrt(x) - Leetcode 69 - Python
NeetCodeIO
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
NeetCodeIO
Optimal Partition of String - Leetcode 2405 - Python
NeetCodeIO
More on: Python for Data
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
Chapters (3)
Read the problem
0:30
Drawing Explanation
4:21
Coding Explanation
🎓
Tutor Explanation
DeepCamp AI