-
-
Save divyang4481/321fc52317029b1983a4d2cf7d57d560 to your computer and use it in GitHub Desktop.
Prefix and Suffix Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WordFilter { | |
private string[] _words; | |
public WordFilter(string[] words) { | |
_words =words; | |
} | |
public int F(string prefix, string suffix) { | |
int ans = -1; | |
var prefixLength =prefix.Length; | |
var suffixLength =suffix.Length; | |
var prefixfound =-1; | |
var suffixfound =-1; | |
var result = new Dictionary<int,int>(); | |
for(var i =0; i< _words.Length; i++){ | |
var ismatch=true; | |
if(_words[i].Length>= prefixLength){ | |
for(var idx =0; idx<prefixLength; idx++ ){ | |
if(_words[i][idx] != prefix[idx]){ | |
ismatch=false; | |
break; | |
} | |
} | |
if(ismatch ){ | |
// prefixfound = prefixLength; | |
result[prefixLength] =i; | |
// ans = System.Math.Max(ans,i); | |
} | |
} | |
if(_words[i].Length>= suffixLength && ismatch){ | |
var wordIdx = _words[i].Length; | |
for(var idx =0; idx < suffixLength ; idx++ ){ | |
if(_words[i][wordIdx - suffixLength + idx] != suffix[idx]){ | |
ismatch=false; | |
break; | |
} | |
} | |
if(ismatch ){ | |
result[_words[i].Length] =i; | |
result[prefixLength] =i; | |
//suffixfound = _words[i].Length; | |
// ans = System.Math.Max(ans,i); | |
} | |
} | |
} | |
return result.Count==0? -1 : result[result.Keys.Max()]; | |
} | |
} | |
/** | |
* Your WordFilter object will be instantiated and called as such: | |
* WordFilter obj = new WordFilter(words); | |
* int param_1 = obj.F(prefix,suffix); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
has time exceed issu