Coding problem
Where a scrambled tag appears
The task
A log scanner looks for a short tag whose letters may arrive in any order, so it must find every stretch of the log that uses exactly the tag's letters.
Complete RearrangementStarts(string text, string pattern). Both strings hold lowercase English letters only, and pattern is not empty. Return, in increasing order, every index i where the pattern.Length letters of text that start at i are a rearrangement of pattern: the same letters, each the same number of times. A text shorter than the pattern gives an empty list.
Signature
public static List<int> RearrangementStarts(string text, string pattern)
Examples
"xyzzyx", "xyz"→[0, 3]. "xyz" starts at 0, and "zyx" at 3 uses the same letters."abcabc", "cab"→[0, 1, 2, 3]. Every three-letter stretch holds one a, one b and one c.
Limits: Up to 600,000 letters of text and a pattern of up to 200,000 letters.
The tests check: an empty text, a text shorter than the pattern, a match of the whole text, overlapping matches, repeated letters, letter counts that differ, a match at the very end, and a 200,000-letter pattern in a 600,000-letter text in time.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public static class Solution
{
// Every start index where the next pattern.Length letters of text are a rearrangement of pattern, in order.
public static List<int> RearrangementStarts(string text, string pattern)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.