← 返回 databricks 的题目列表Find the First Anagram Substring
类型:online_judge
Given two strings, text and pattern, find the earliest contiguous substring in text that contains exactly the same characters with the same frequencies as pattern, regardless of character order.
Return the starting index of that substring. Return -1 if no such substring exists.
Indices are zero-based.
Example 1:
Input: text = "cbaebabacd", pattern = "abc"
Output: 0
Explanation: text[0:3] = "cba", which is an anagram of "abc".
Example 2:
Input: text = "abab", pattern = "ab"
Output: 0
Explanation: The earliest matching substring is "ab".
Example 3:
Input: text = "abcdef", pattern = "gh"
Output: -1
Constraints:
0 <= len(text) <= 10^5
1 <= len(pattern) <= 10^5
Characters may come from an arbitrary Unicode character set.
Return -1 when len(pattern) > len(text).
Design an algorithm with O(len(text) + len(pattern)) time complexity.
Example
Input
cbaebabacd
abc
Output
0