Microsoft Interview Question
SDE-2sTeam: STB and MVO
Country: India
Interview Type: In-Person
List<int> IsMatch ( char[] Text, char[] P)
{
List<int> indicesList = new List<>;
a = Text.Length;
b = P.Length;
int checkSumTemp = 0;
int checkSumP = 0;
for(int i = 0; i< b; i++)
{ checkSumTemp += (int)Text[i];
checkSumP += (int)P[i];
}
for(int i = 0; i<=a-b;i++)
{
if(checkSumTemp == checkSumP)
{
for(int j = i; j<i+b; j++)
{
if(Text[j]!=P[j-i]) break;
}
if ( j==i+b )
{
indicesList.Add(i);
}
}
checkSumTemp -= Text[i];
checkSumTemp += Text[i+b];
}
return indicesList;
}
String pattern = "AAB";
String text = "AACBAABDJHAAB AAB";
int patternLength = pattern.length();
int startIndex = 0;
for (int textIndex = 0; textIndex < text.length(); textIndex++) {
if (textIndex + patternLength <= text.length()) {
String subString = text.substring(textIndex, textIndex
+ patternLength);
if (pattern.equalsIgnoreCase(subString)) {
System.out.println(textIndex);
}
}
}
Standard pattern matching problem. Use Rabin Karp or KMP algorithm.
- Anonymous April 10, 2013