string bigstring = "this is a big string";
string[] smallstrings = { "this", "yo", "is", "a", "bigger", "string", "kappa" };
List result = MultistringSearch(bigstring, smallstrings);
foreach (bool b in result)
{
Console.WriteLine(b);
}
//Expected output: [true, false, true, true, false, true, false]
Solution 1.
public static ListMultistringSearch(string bigstring, string[] smallstrings) { List result = new List (); foreach (string smalString in smallstrings) { result.Add(isContain(smalString, bigstring)); } return result; } public static bool isContain(string smallString, string bigString) { for (int i = 0; i <= bigString.Length - smallString.Length; i++) { int j; for (j = 0; j < smallString.Length; j++) { if (bigString[i + j] != smallString[j]) { break; } } if (j == smallString.Length) { return true; } } return false; }
Solution 2:
public static bool IsContain(string smallString, string bigString)
{
for (int i = 0; i < bigString.Length; i++)
{
if (i + smallString.Length > bigString.Length)
{
break;
}
if(IsContain(smallString, bigString, i))
{
return true;
}
}
return false;
}
private static bool IsContain(string smallString, string bigString, int startIdx)
{
int leftBigidx = startIdx;
int rightBigIdx = startIdx + smallString.Length - 1;
int leftSmallIdx = 0;
int rightSmallIdx = smallString.Length - 1;
while (leftBigidx <= rightBigIdx)
{
if (bigString[leftBigidx] != smallString[leftSmallIdx] ||
bigString[rightBigIdx] != smallString[rightSmallIdx])
{
return false;
}
leftBigidx++;
rightBigIdx--;
leftSmallIdx++;
rightSmallIdx--;
}
return true;
}
Test case
Test Case 1
{
"bigString": "this is a big string",
"smallStrings": ["this", "yo", "is", "a", "bigger", "string", "kappa"]
}
Test Case 2
{
"bigString": "abcdefghijklmnopqrstuvwxyz",
"smallStrings": ["abc", "mnopqr", "wyz", "no", "e", "tuuv"]
}
Test Case 3
{
"bigString": "abcdefghijklmnopqrstuvwxyz",
"smallStrings": ["abcdefghijklmnopqrstuvwxyz", "abc", "j", "mnopqr", "pqrstuvwxyz", "xyzz", "defh"]
}
Test Case 4
{
"bigString": "hj!)!%Hj1jh8f1985n!)51",
"smallStrings": ["%Hj7", "8f198", "!)5", "!)!", "!!", "jh81", "j181hf"]
}
Test Case 5
{
"bigString": "Mary goes to the shopping center every week.",
"smallStrings": ["to", "Mary", "centers", "shop", "shopping", "string", "kappa"]
}
Test Case 6
{
"bigString": "adcb akfkw afnmc fkadn vkaca jdaf dacb cdba cbda",
"smallStrings": ["abcd", "acbd", "adbc", "dabc", "cbda", "cabd", "cdab"]
}
Test Case 7
{
"bigString": "test testing testings tests testers test-takers",
"smallStrings": ["tests", "testatk", "testiing", "trsatii", "test-taker", "test"]
}
Test Case 8
{
"bigString": "ndbajwhfawkjljkfaopwdlaawjk dawkj awjkawkfjhkawk ahjwkjad jadfljawd",
"smallStrings": ["abc", "akwbc", "awbc", "abafac", "ajjfbc", "abac", "jadfl"]
}
Test Case 9
{
"bigString": "Is this particular test going to pass or is it going to fail? That is the question.",
"smallStrings": ["that", "the", "questions", "goes", "mountain", "passes", "passed", "going", "is"]
}
Test Case 10
{
"bigString": "Everything in this test should fail.",
"smallStrings": ["everything", "inn", "that", "testers", "shall", "failure"]
}
Test Case 11
{
"bigString": "this ain't a big string",
"smallStrings": ["this", "is", "yo", "a", "bigger"]
}
Test Case 12
{
"bigString": "bbbabb",
"smallStrings": ["bbabb"]
}
No comments:
Post a Comment