Redistribute Strings to Equal Length with HashMap

Day 38 of Daily DSA 🚀 Solved LeetCode 1897: Redistribute Characters to Make All Strings Equal ✅ Problem: Given an array of strings, determine if you can make every string equal by moving any character from one string to any position in another string. Rules: * You can pick two distinct indices i and j and move any character from words[i] to any position in words[j] * Any number of operations are allowed * Return true if all strings can be made equal, false otherwise Approach: Used a HashMap to count the total frequency of every character across all strings. If every character's count is divisible by the number of strings, equal redistribution is possible. Steps: 1. Concatenate all strings into one using StringBuilder 2. Count the frequency of each character using a HashMap 3. For every character count, check if it is divisible by the number of words 4. If any count is not divisible → return false 5. Otherwise → return true ⏱ Complexity: • Time: O(n * m) — n = number of words, m = average word length • Space: O(1) — at most 26 characters in the map 📊 LeetCode Stats: • Runtime: 12 ms (Beats 17.93%) • Memory: 46.63 MB A brilliant insight — redistribution is just a divisibility check! No complex simulation needed. #DSA #LeetCode #Java #HashMap #Strings #CodingJourney #ProblemSolving

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories