Substring algorithm

后端 未结 11 1291
小蘑菇
小蘑菇 2021-02-11 03:38

Can someone explain to me how to solve the substring problem iteratively?

The problem: given two strings S=S1S2S

11条回答
  •  我在风中等你
    2021-02-11 03:53

    Here is my PHP variation that includes a check to make sure the Needle does not exceed the Haystacks length during the search.

    = $len) { return false; }
                                    if($needle[$j] != $haystack[$i+$j]) {
                                            $found = false;
                                            continue;
                                    }
                            }
                            if($found) {
                                    echo " . . . . . . SUCCESS!!!! startPos: $i\n";
                                    return true;
                            }
                    }
            }
            echo " . . . . . . FAILURE!\n" ;
            return false;
    }
    
    assert(substring("haystack","hay"));
    assert(!substring("ack","hoy"));
    assert(substring("hayhayhay","hayhay"));
    assert(substring("mucho22","22"));
    assert(!substring("str","string"));
    ?>
    

    Left in some echo's. Remove if they offend you!

提交回复
热议问题