I am trying a problem in LeetCode and it seems almost done. This is what I’ve done so far: Fiddle and problem link – 30. Substring with Concatenation of All Words. Following is the code snippet that I did so far:
static IList < int > FindSubstring(string s, string[] words) {
var list = new List < IList < string >> ();
DoPermutation(words, 0, words.Length - 1, list);
return TrackIndexes(s, list);
}
static IList < IList < string >> DoPermutation(string[] str, int start, int end, IList < IList < string >> list) {
if (start == end) {
list.Add(new List < string > (str));
} else {
for (var i = start; i <= end; i++) {
Swap(ref str[start], ref str[i]);
DoPermutation(str, start + 1, end, list);
Swap(ref str[start], ref str[i]);
}
}
return list;
}
static void Swap(ref string a, ref string b) {
var temp = a;
a = b;
b = temp;
}
static List < int > TrackIndexes(string pattern, IList < IList < string >> aLst) {
int count = 0;
List < string > strings;
List < int > indexes = new List < int > ();
foreach(var item in aLst) {
string str = "";
strings = new List < string > {
String.Concat(item)
};
foreach(var list in item) {
str += list;
if (str.Length == strings[0].Length && pattern.Contains(str)) {
indexes.AddRange(AllIndexesOf(pattern, str));
count += 1;
}
}
}
indexes.Sort();
return indexes.Distinct().ToList();
}
public static int[] AllIndexesOf(string str, string substr, bool ignoreCase = false)
{
if (string.IsNullOrWhiteSpace(str) || string.IsNullOrWhiteSpace(substr))
{
throw new ArgumentException("String or substring is not specified.");
}
var indexes = new List<int>();
int index = 0;
while ((index = str.IndexOf(substr, index, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)) != -1)
{
indexes.Add(index++);
}
return indexes.ToArray();
}
The issue I’ve, is the following input:
s = "pjzkrkevzztxductzzxmxsvwjkxpvukmfjywwetvfnujhweiybwvvsrfequzkhossmootkmyxgjgfordrpapjuunmqnxxdrqrfgkrsjqbszgiqlcfnrpjlcwdrvbumtotzylshdvccdmsqoadfrpsvnwpizlwszrtyclhgilklydbmfhuywotjmktnwrfvizvnmfvvqfiokkdprznnnjycttprkxpuykhmpchiksyucbmtabiqkisgbhxngmhezrrqvayfsxauampdpxtafniiwfvdufhtwajrbkxtjzqjnfocdhekumttuqwovfjrgulhekcpjszyynadxhnttgmnxkduqmmyhzfnjhducesctufqbumxbamalqudeibljgbspeotkgvddcwgxidaiqcvgwykhbysjzlzfbupkqunuqtraxrlptivshhbihtsigtpipguhbhctcvubnhqipncyxfjebdnjyetnlnvmuxhzsdahkrscewabejifmxombiamxvauuitoltyymsarqcuuoezcbqpdaprxmsrickwpgwpsoplhugbikbkotzrtqkscekkgwjycfnvwfgdzogjzjvpcvixnsqsxacfwndzvrwrycwxrcismdhqapoojegggkocyrdtkzmiekhxoppctytvphjynrhtcvxcobxbcjjivtfjiwmduhzjokkbctweqtigwfhzorjlkpuuliaipbtfldinyetoybvugevwvhhhweejogrghllsouipabfafcxnhukcbtmxzshoyyufjhzadhrelweszbfgwpkzlwxkogyogutscvuhcllphshivnoteztpxsaoaacgxyaztuixhunrowzljqfqrahosheukhahhbiaxqzfmmwcjxountkevsvpbzjnilwpoermxrtlfroqoclexxisrdhvfsindffslyekrzwzqkpeocilatftymodgztjgybtyheqgcpwogdcjlnlesefgvimwbxcbzvaibspdjnrpqtyeilkcspknyylbwndvkffmzuriilxagyerjptbgeqgebiaqnvdubrtxibhvakcyotkfonmseszhczapxdlauexehhaireihxsplgdgmxfvaevrbadbwjbdrkfbbjjkgcztkcbwagtcnrtqryuqixtzhaakjlurnumzyovawrcjiwabuwretmdamfkxrgqgcdgbrdbnugzecbgyxxdqmisaqcyjkqrntxqmdrczxbebemcblftxplafnyoxqimkhcykwamvdsxjezkpgdpvopddptdfbprjustquhlazkjfluxrzopqdstulybnqvyknrchbphcarknnhhovweaqawdyxsqsqahkepluypwrzjegqtdoxfgzdkydeoxvrfhxusrujnmjzqrrlxglcmkiykldbiasnhrjbjekystzilrwkzhontwmehrfsrzfaqrbbxncphbzuuxeteshyrveamjsfiaharkcqxefghgceeixkdgkuboupxnwhnfigpkwnqdvzlydpidcljmflbccarbiegsmweklwngvygbqpescpeichmfidgsjmkvkofvkuehsmkkbocgejoiqcnafvuokelwuqsgkyoekaroptuvekfvmtxtqshcwsztkrzwrpabqrrhnlerxjojemcxel",
words = ["dhvf","sind","ffsl","yekr","zwzq","kpeo","cila","tfty","modg","ztjg","ybty","heqg","cpwo","gdcj","lnle","sefg","vimw","bxcb"]
The code I’ve, creates a list of possible permutations in the DoPermutation method and for the above input, it generates a list of strings that gets out of memory error. I am not sure if anything can be altered using the code snippet I’ve.
For now, my code passes 151 out of 180 test cases. Is there anyway using the existing code snippet to get rid of the memory exception or I’ve to rethink for the problem?
Objective: The main objective of the code is to create possible permutation for list of strings and match those string in a pattern as substring.