/* * @Author: LetMeFly * @Date: 2024-12-20 21:06:55 * @LastEditors: LetMeFly.xyz * @LastEditTime: 2024-12-20 21:14:34 */ voidcount(int times[], char s[], int l, int r) { for (int i = l; i < r; i++) { times[s[i] - 'a']++; } }
intsame26(int a[], int b[]) { for (int i = 0; i < 26; i++) { if (a[i] != b[i]) { return0; } } return1; }
intminAnagramLength(char* s) { int n = strlen(s); for (int ans = 1; ans < n; ans++) { if (n % ans) { continue; } int should[26] = {0}; count(should, s, 0, ans); for (int i = ans; i < n; i += ans) { int now[26] = {0}; count(now, s, i, i + ans); if (!same26(should, now)) { goto loop; } } return ans; loop:; } return n; }
classSolution: defminAnagramLength(self, s: str) -> int: for ans inrange(1, len(s)): iflen(s) % ans: continue should = Counter(s[:ans]) ok = True for i inrange(ans, len(s), ans): if should != Counter(s[i:i + ans]): ok = False break if ok: return ans returnlen(s)
funccount(s string, l, r int) []int { ans := make([]int, 26) for i := l; i < r; i++ { ans[s[i] - 'a']++ } return ans }
funcsame26(a, b []int)bool { for i := range a { if a[i] != b[i] { returnfalse; } } returntrue; }
funcminAnagramLength(s string)int { for ans := 1; ans < len(s); ans++ { iflen(s) % ans != 0 { continue } should := count(s, 0, ans) ok := true for i := ans; i < len(s); i += ans { if !same26(should, count(s, i, i + ans)) { ok = false break } } if ok { return ans } } returnlen(s) }