Minimum Length of Anagram Concatenation - Problem

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".

Input & Output

Example 1 — Equal Character Frequencies
$ Input: s = "abab"
Output: 2
💡 Note: String can be split as "ab" + "ab". Both segments are anagrams of "ab", so minimum length is 2.
Example 2 — Multiple Character Pattern
$ Input: s = "aabaab"
Output: 3
💡 Note: String can be split as "aab" + "aab". Both segments are identical anagrams, so minimum length is 3.
Example 3 — Single Character
$ Input: s = "aaaa"
Output: 1
💡 Note: All characters are the same, so the minimum pattern is just "a" repeated 4 times.

Constraints

  • 1 ≤ s.length ≤ 105
  • s consists of lowercase English letters

Visualization

Tap to expand
Minimum Length of Anagram Concatenation INPUT String s: a b a b 0 1 2 3 Character Count: a: 2 b: 2 Length: 4 Divisors: 1, 2, 4 s = "abab" Concatenation of anagrams ALGORITHM STEPS 1 Find Divisors Get all divisors of len(s) 2 Try Each Divisor k Start from smallest (1,2,4) 3 Check Anagram Split Split s into n/k chunks 4 Verify All Anagrams Compare char frequencies Testing k=2: ab ab OK "ab" and "ab" are anagrams Valid! Return k=2 FINAL RESULT Minimum anagram length t: 2 String t = "ab" (or "ba") Verification: "abab" = "ab" + "ab" Both chunks are anagrams of each other - OK Output: 2 Key Insight: The length of t must be a divisor of len(s). Try divisors from smallest to largest: if s can be split into equal anagram chunks of size k, return k. Time: O(n * d) where d = number of divisors. TutorialsPoint - Minimum Length of Anagram Concatenation | Optimal Solution
Asked in
Google 25 Microsoft 18 Amazon 15
33.0K Views
Medium Frequency
~15 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen