Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created February 19, 2018 02: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 jianminchen/d597afdd55b000ed363e3898e29b194c to your computer and use it in GitHub Desktop.
Save jianminchen/d597afdd55b000ed363e3898e29b194c to your computer and use it in GitHub Desktop.
Hashset mock interview algorithm
/*
Given a set of strings, e.g. {“one”, “cat”, “two”, “four”},
and a target string, e.g. “fouroneone”, return true if the target string is
composed of elements from the set.
“onecat” -> true
“fouron” -> false
“twone” -> false
one, cat, two, four
a target string fouroneone
target string is composed of elementes from the set
*/
bool isValidConcatination(string input, unordered_map<string> UMStr,
unordered_map<string, bool> Visited){
if(input.size() == 0){
return true;
}
if(Visited.count(input)){ // here I'm checking if I have alredy vistited that string...if yes I return from here...
return Visited[input];
}
for(int len = 1; len < input.size(); len++)
{
string substr = input.substr(0, len); // all possible substring
// means valid string or here we can use trie to check is alid string..
if(UMStr.count(substr))
{
if(isValidConcatination(input.substr(len), UMstr))
->put something true in the memo
return true;
}
}
Visited[input] = false; // I'm in doubt...
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment