diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /redistribute_char_str_eq.py | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'redistribute_char_str_eq.py')
-rw-r--r-- | redistribute_char_str_eq.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/redistribute_char_str_eq.py b/redistribute_char_str_eq.py new file mode 100644 index 0000000..c76b9b2 --- /dev/null +++ b/redistribute_char_str_eq.py @@ -0,0 +1,32 @@ +# 1897. Redistribute Characters to Make All Strings Equal +from collections import defaultdict + +""" +given an array of strings 'words' in one operation, pick two distinct indices +'i' and 'j' where 'words[i]' is a non empty string and ove any character from +words[i] to any position in 'words[j]'. return 1 if you can make every string +in 'words' equal using any number of operations, and 0 otherwise. +""" + + +class Solution(object): + def makeEqual(self, words): + """ + :type words: List[str] + :rtype: bool + """ + cnt = defaultdict(int) + for w in words: + for c in w: + cnt[c] += 1 + n = len(words) + for val in cnt.values(): + if val % n != 0: + return False + return True + + +if __name__ == "__main__": + obj = Solution() + print(obj.makeEqual(words=["abc", "aabc", "bc"])) + print(obj.makeEqual(words=["ab", "a"])) |