Skip to content

Instantly share code, notes, and snippets.

@divyang4481
Created May 9, 2021 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divyang4481/321fc52317029b1983a4d2cf7d57d560 to your computer and use it in GitHub Desktop.
Save divyang4481/321fc52317029b1983a4d2cf7d57d560 to your computer and use it in GitHub Desktop.
Prefix and Suffix Search
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);
*/
@divyang4481
Copy link
Author

has time exceed issu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment