Ctrl K

Glued Shuffled Copies

Medium
Someone took a word, made several copies of it, shuffled the letters inside each copy, and glued the copies one after another. The result is the string text. The original word is unknown. Return the smallest length it could have had. The word may be the whole string: a single unshuffled copy is a valid option too.
Example 1
text="xyyxyxxy" → 2

Blocks of length 2 are "xy", "yx", "yx", "xy", all rearrangements of "xy". Length 1 fails because the letters differ.

Example 2
text="ppppqq" → 6

With length 2 the blocks "pp", "pp", "qq" differ in letters, and with length 3 so do "ppp" and "pqq". Only the whole string remains: 6.

Example 3
text="abcab" → 5

The length 5 is prime, so the word is the whole string.

Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Hint for a Digit Guess MediumCharacters Grouped by Count MediumMost Searched Words MediumNo Twin Letters Side by Side MediumDomain Visit Rollup Medium
loading Python…
Input
text="xyyxyxxy"
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 76 tests on the server, hidden ones included.