Student Interview Question
fresherssCountry: United States
Think in terms of automata. Every letter in the sample string is a state, which gets activated when a letter matches, and then it waits for the next state, which is the next letter.
Thus, for a sample string -> s[i] where s[i] denotes i'th letter, s[0] is the first letter, we have this automata in place ( axiomatic ) with s[-1] is the last letter :
( anything not s[0] ) * -> s[0] -> ( anything not s[1] ) * -> s[1] .... -> s[-1] -> end
We are confused about whether or not s[0] should be the first letter and s[-1] should be the last letter of the text string - but those are easy to adjust. Thus, the automata can be coded as (in ZoomBA):
def recognize( pattern_string , text_string ){
cur_pattern_index = 0
for ( char : text_string ){
continue ( char != pattern_string[ cur_pattern_index ] )
cur_pattern_index += 1 // matched, so move to next pattern letter
}
return cur_pattern_index == size( pattern_string )
}
println ( recognize('Rdd Waitn' , 'Redmond, Washington' ) )
println ( recognize('Rdd Waixtn' , 'Redmond, Washington' ) )
- srterpe December 04, 2016