Welcome to Subscribe On Youtube
3138. Minimum Length of Anagram Concatenation
Description
You are given a string s
, which is known to be a concatenation of anagrams of some string t
.
Return the minimum possible length of the string t
.
An anagram is formed by rearranging the letters of a string. For example, "aab", "aba", and, "baa" are anagrams of "aab".
Example 1:
Input: s = "abba"
Output: 2
Explanation:
One possible string t
could be "ba"
.
Example 2:
Input: s = "cdef"
Output: 4
Explanation:
One possible string t
could be "cdef"
, notice that t
can be equal to s
.
Constraints:
1 <= s.length <= 105
s
consist only of lowercase English letters.
Solutions
Solution 1
-
class Solution { private int n; private char[] s; private int[] cnt = new int[26]; public int minAnagramLength(String s) { n = s.length(); this.s = s.toCharArray(); for (int i = 0; i < n; ++i) { ++cnt[this.s[i] - 'a']; } for (int i = 1;; ++i) { if (n % i == 0 && check(i)) { return i; } } } private boolean check(int k) { for (int i = 0; i < n; i += k) { int[] cnt1 = new int[26]; for (int j = i; j < i + k; ++j) { ++cnt1[s[j] - 'a']; } for (int j = 0; j < 26; ++j) { if (cnt1[j] * (n / k) != cnt[j]) { return false; } } } return true; } }
-
class Solution { public: int minAnagramLength(string s) { int n = s.size(); int cnt[26]{}; for (char c : s) { cnt[c - 'a']++; } auto check = [&](int k) { for (int i = 0; i < n; i += k) { int cnt1[26]{}; for (int j = i; j < i + k; ++j) { cnt1[s[j] - 'a']++; } for (int j = 0; j < 26; ++j) { if (cnt1[j] * (n / k) != cnt[j]) { return false; } } } return true; }; for (int i = 1;; ++i) { if (n % i == 0 && check(i)) { return i; } } } };
-
class Solution: def minAnagramLength(self, s: str) -> int: def check(k: int) -> bool: for i in range(0, n, k): cnt1 = Counter(s[i : i + k]) for c, v in cnt.items(): if cnt1[c] * (n // k) != v: return False return True cnt = Counter(s) n = len(s) for i in range(1, n + 1): if n % i == 0 and check(i): return i
-
func minAnagramLength(s string) int { n := len(s) cnt := [26]int{} for _, c := range s { cnt[c-'a']++ } check := func(k int) bool { for i := 0; i < n; i += k { cnt1 := [26]int{} for j := i; j < i+k; j++ { cnt1[s[j]-'a']++ } for j, v := range cnt { if cnt1[j]*(n/k) != v { return false } } } return true } for i := 1; ; i++ { if n%i == 0 && check(i) { return i } } }
-
function minAnagramLength(s: string): number { const n = s.length; const cnt: Record<string, number> = {}; for (let i = 0; i < n; i++) { cnt[s[i]] = (cnt[s[i]] || 0) + 1; } const check = (k: number): boolean => { for (let i = 0; i < n; i += k) { const cnt1: Record<string, number> = {}; for (let j = i; j < i + k; j++) { cnt1[s[j]] = (cnt1[s[j]] || 0) + 1; } for (const [c, v] of Object.entries(cnt)) { if (cnt1[c] * ((n / k) | 0) !== v) { return false; } } } return true; }; for (let i = 1; ; ++i) { if (n % i === 0 && check(i)) { return i; } } }