Google Interview Question
Software Engineer / Developers<pre lang="" line="1" title="CodeMonkey34921" class="run-this">Build a hash map for the file with keys as individual characters and values as list of positions.
current_position = 0
Read the input string.
look up for the character n hash table
if not present
return false
else
if any position in list is greater than current_position
set current_position to position found in hash map
continue
else
return false
return true</pre><pre title="CodeMonkey34921" input="yes">
</pre>
bytestorm's idea is correct. However there's no need to implement full Finite Automata due to the nature of this problem. Simply search for each character of pattern in the given filename and return true only if pattern can be exhausted.
Code in Java:
String filename = ...
String pattern = ...
int p1 = 0, p2 = 0;
while(p1 >= 0 && p2 < pattern.length){
char c = pattern.charAt(p2);
int tmp = filename.indexOf(c, p1);
if(tmp < 0){
return false;
}
p1 = tmp + 1;
p2 ++;
}
return true;
Looks like a problem of Finite Automata. The test pattern needs to be converted into f.*l.*t.*_.*f.*l.* Try to match the given string on this pattern.
- bytestorm August 21, 2011