In cybersecurity and system administration, analyzing vast streams of log data is crucial for detecting anomalies, security breaches, or system failures. Often, specific sequences of characters within a log entry indicate a critical event or a known malicious pattern. Your task is to develop an efficient algorithm that can quickly locate the first occurrence of such a pattern within a much larger log stream.
Given two strings, logStream (representing the entire log data) and anomalyPattern (the specific pattern you're searching for), your goal is to find the starting index of the very first time anomalyPattern appears as a contiguous substring within logStream. If the pattern is not found anywhere in the log stream, you should report this by returning -1.
For example, if logStream is "sysalertservicefailurerestart" and anomalyPattern is "servicefailure", the pattern starts at index 9.
This problem requires an algorithm that is highly efficient, especially when dealing with very long log streams and potentially repetitive patterns. Consider using advanced string matching techniques to solve this challenge optimally.
The first line of input contains an integer 'T', denoting the number of test cases.
For each test case:
logStream.anomalyPattern.For each test case, your program should output a single integer on a new line. This integer should be the 0-based index of the first occurrence of anomalyPattern in logStream. If anomalyPattern is not found, output -1.
1 <= T <= 501 <= logStream.length, anomalyPattern.length <= 105logStream and anomalyPattern consist only of lowercase English alphabets ('a'-'z').Example 1: Basic Match
logStream: "systemalertcriticalprocessfailure" anomalyPattern: "critical" Explanation: The pattern "critical" starts at index 11 of "systemalertcriticalprocessfailure". Answer: 11Example 2: No Match
logStream: "webserverstartupok" anomalyPattern: "databaseerror" Explanation: The pattern "databaseerror" is not present in "webserverstartupok". Answer: -1Example 3: Match at Beginning
logStream: "errorlogid456eventdetected" anomalyPattern: "errorlog" Explanation: The pattern "errorlog" starts right at the beginning of the log stream, at index 0. Answer: 0
String & Tries
Start coding and your submissions will appear here.