Redistribute Characters to Make All Strings Equal - Leetcode 1897 - Python
Key Takeaways
The video demonstrates how to solve the Leetcode 1897 problem, Redistribute Characters to Make All Strings Equal, using a hashmap to count character occurrences and checking divisibility by the length of the words array.
Full Transcript
hey everyone welcome back and let's write some more neat code today so today let's solve the problem redistribute characters to make all strings equal we're given an array of words and we can make one particular operation and that is from any of these words like this one we can take a character and then move it into a different word in any position in that word so not super complicated and we want to return true if it's possible that we can make every string in the list of words equal to each other so basically we want all of these words to be the same and in this case we can do that by taking one of these A's and putting it at the beginning here because that will make this string ABC just like this and just like this so at first this problem does seem more complicated than it is because if you really take this operation literally you will have to do quite a few operations it's not simple like we could take this this a and put it in any position in either of these words that's not simple that is going to be very very complex in terms of time complexity but it's actually more simple than that this problem is more simple if you notice this operation actually gives us a lot of flexibility what it does for us is we can take any of these characters move it into a different word and then there's no restriction that we can't just take this exact same character and then move it back but maybe in a different position so in other words this operation allows us to take any of these characters and rearrange them in any way that we want we can take any of these characters and rearrange them if we wanted to make this b a c that's fine the reason this is important is because it basically takes this problem and reduces it to a much simpler one if we want to know if we can create a list of words that is the same length as this one where every word is the exact same the question we should really be asking ourselves is do we have enough copies of every single character such that that is possible so it really becomes a counting game we've reduced this problem to just counting the occurrences of each character so once we do that and probably the best data structure for us to do that is going to be with a hashmap so for every character we're going to count the occurrences so suppose for lowercase a we count that there are three occurrences of it for lowercase b we find that there are three occurrences of it and same for lowercase C there are three occurrences of it and in the input we're given three words so what we find is that for each word we can give the word a single a a single b and a single C how we arrange those characters doesn't really matter whether it's a b c or b a c because as long as each word has the same count of each character it's possible to arrange them such that they're all equal so this is pretty much all you need to know to solve this problem but it's possible that maybe we actually had six C's and 3 B's and 3 A's does this make it possible for us to arrange every word such that they're the same well we have three words remember instead of giving each word one C we can give each word two C's and that's because six divides evenly with three basically if you take six mod it by three you get a remainder of zero that means that three can divide six evenly so really what we're going to be doing is counting the occurrences of each character and then ensuring that that count is divisible by the length of the words the length of the words array not the length of an individual word but the length of the words array which in this case is three if we had a five here instead for the count of C we we know that three does not divide five we'll get a remainder of two so in this case it would not be possible for us to uh distribute the characters so in that case we would return false but in the other cases that we were talking about we would return true so knowing all of that the time complexity of this problem just becomes a matter of counting the occurrences of each character which let's just say is going to be Big O of n let's say n is the total number of characters in the string or if we want to say n is the number of words then we can say n * m where maybe m is the length of the longest word in that case the space complexity would be the number of unique characters which let's say is 26 I believe for this problem so technically the space complexity is going to be constant now let's code this up so like I said what we want to do is just count the occurrences of each character the easiest way to do that is with a hashmap and I'm going to create create a default dictionary actually makes things a bit easier for us in Python because then when we iterate over every word in the list of words and then for each word we iterate over every character in that word then when we want to increment the count of that particular character we can just say plus equal 1 and you might think well what if this wasn't even initialized how can we add one to something that doesn't even exist well that's what a default dictionary does if the value is not initialized it will be assumed to be the integer zero that helps us out there so now that we've built that hashmap we want to go through every character in that hashmap and we want not the character we want the count of that character which we can get pretty easily like this and we want to check is this divisible by the length of words and if it's not like if this is greater than zero or we can just in Python implicitly check if this is nonzero like this if this is the case we know immediately we can return false because it's just not possible to distribute the characters but if we go through every character and check that the count is divisible then out here we can return true it is possible so this is the entire solution let's run it to make sure that it works as you can see on the left yes it does and it's pretty efficient if you found this helpful please like And subscribe if you're preparing for coding interviews check out NE code. 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/
🥷 Discord: https://discord.gg/ddjKRXPqtk
🐦 Twitter: https://twitter.com/neetcode1
🐮 Support the channel: https://www.patreon.com/NEETcode
⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf
💡 DYNAMIC PROGRAMMING PLAYLIST: https://www.youtube.com/watch?v=73r3KWiEvyk&list=PLot-Xpze53lcvx_tjrr_m2lgD2NsRHlNO&index=1
Problem Link: https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/
0:00 - Read the problem
0:10 - Drawing Explanation
4:51 - Coding Explanation
leetcode 1897
#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: Algorithm Basics
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:10
Drawing Explanation
4:51
Coding Explanation
🎓
Tutor Explanation
DeepCamp AI