cswork / OAOpen practice workspace

DatabricksOA #18

Find First Anagram Index

Estimated MediumJudge-ready

Topics (AI-labelled)Two PointersHashing

Given strings s and pattern of lowercase English letters, return the starting index of the earliest contiguous substring of s whose character frequencies exactly match those of pattern; if none exists, return -1.

Standard input/output and this site's supplementary ranges follow this problem's description.

Input

Two lines, containing s and pattern respectively. This site restricts the character set to ASCII lowercase letters; 1≤|s|,|pattern|≤200000.

Output

Output the earliest starting index (0-based); if none exists, output -1.

Example 1

Input:

cbaebabacd
abc

Output:

0

Example 2

Input:

abab
ab

Output:

0

Example 3

Input:

abc
cba

Output:

0

Editorial

Loading solution…