FastPrepMinimum Anagram Period

Minimum Anagram Period

Visa logoVisa● MediumNEW GRADOA
Learn

Problem statement

You are given a string inputStr containing lowercase English letters.

Choose a positive block length p that divides inputStr.length. Split the entire string into consecutive blocks of length p. A block length is valid when every block is an anagram of every other block, meaning that all blocks have identical character frequencies.

Return the minimum valid block length.

Function

getAnagramPeriod(inputStr: String) → int

Examples

Example 1

inputStr = "abcbcacba"return = 3

Split the string into abc, bca, and cba. The three blocks are anagrams, and no smaller block length works.

Example 2

inputStr = "ababbaab"return = 2

The blocks are ab, ab, ba, and ab. Every block has one a and one b.

Example 3

inputStr = "aaaa"return = 1

Every one-character block is the string a, so the minimum valid length is 1.

Constraints

  • 1 <= inputStr.length <= 10^5.
  • inputStr contains only lowercase English letters.

More Visa problems

See Visa hiring insights
public int getAnagramPeriod(String inputStr) {
  // write your code here
}
inputStr"abcbcacba"
expected3
Checking account…