|
| 1 | +--- |
| 2 | +title: 3138. Minimum Length of Anagram Concatenation.md |
| 3 | +date: 20/12/2024 |
| 4 | +tags: |
| 5 | + - Python |
| 6 | + - Prefix Sum |
| 7 | + - Hash Table |
| 8 | + - String |
| 9 | + - Counting |
| 10 | +abbrlink: d1339d55 |
| 11 | +--- |
| 12 | + |
| 13 | +# Description: |
| 14 | + |
| 15 | +https://leetcode.cn/problems/minimum-length-of-anagram-concatenation/description/?envType=daily-question&envId=2024-12-20 |
| 16 | +You are given a string `s`, which is known to be a concatenation of anagrams of some string `t`. |
| 17 | + |
| 18 | +Return the minimum possible length of the string `t`. |
| 19 | + |
| 20 | +An anagram is formed by rearranging the letters of a string. For example, "aab", "aba", and, "baa" are anagrams of "aab". |
| 21 | + |
| 22 | +#### Example 1: |
| 23 | + |
| 24 | + Input: s = "abba" |
| 25 | + |
| 26 | + Output: 2 |
| 27 | + |
| 28 | + Explanation: |
| 29 | + |
| 30 | + One possible string t could be "ba". |
| 31 | + |
| 32 | +#### Example 2: |
| 33 | + |
| 34 | + Input: s = "cdef" |
| 35 | + |
| 36 | + Output: 4 |
| 37 | + |
| 38 | + Explanation: |
| 39 | + |
| 40 | + One possible string t could be "cdef", notice that t can be equal to s. |
| 41 | + |
| 42 | +# Thinking: |
| 43 | +Since the problem naturally suggests using a counting method (`Counter`), we need to find the minimum substring for each string. For example, for `abba`, the result is `ab`; for `cdef`, it's `cdef`. |
| 44 | +We iterate from length `1` (a single character) onwards, slicing the string to get the current substring. |
| 45 | + |
| 46 | +Initially, we compute the character count for the original string using `Counter`, which gives us a dictionary of character frequencies. |
| 47 | +Next, we only need to check if the count of each character in the current substring multiplied by `n/k` equals the count in the original string (i.e., whether repeating the current substring x times equals the original string). |
| 48 | + |
| 49 | +由于题意很容易联想到这道题要进行计数(Counter). 我们需要找到每个字符串的最小子串,`abba`为`ab`, `cdef`为`cdef`. 于是我们从长度0(即单个字符)开始遍历,期间通过切片的形式来获取当前子串。 |
| 50 | +因为最开始我们对原始字符串进行了Counter,得到了字符数量和字符对应的字典。接下来我们只需要判断当前子串的Counter到的某个字符的数值乘以`n/k`是否等于原始字符串的Counter的值即可(即当前子串乘以x倍是否等于源字符串)。 |
| 51 | + |
| 52 | +# Code: |
| 53 | +```python |
| 54 | +import collections |
| 55 | +class Solution: |
| 56 | + def minAnagramLength(self, s: str) -> int: |
| 57 | + def check(k: int) -> bool: |
| 58 | + # 遍历字符串 s,每次取长度为 k 的子串 |
| 59 | + # Iterate over the string `s`, taking substrings of length `k` |
| 60 | + for i in range(0, n, k): |
| 61 | + # 统计每个字符出现的次数 |
| 62 | + # Count the occurrences of each character in the current substring |
| 63 | + cnt1 = collections.Counter(s[i: i + k]) |
| 64 | + for c, v in cnt.items(): |
| 65 | + # 如果每个字符出现的次数乘以 n/k != cnt[] return False |
| 66 | + # If the count of any character multiplied by (n // k) != the original count, return False |
| 67 | + if cnt1[c] * (n // k) != v: |
| 68 | + return False |
| 69 | + return True |
| 70 | + |
| 71 | + cnt = collections.Counter(s) |
| 72 | + n = len(s) |
| 73 | + for i in range(1, n+1): |
| 74 | + if n % i == 0 and check(i): |
| 75 | + return i |
| 76 | +``` |
0 commit comments