Match sub-string within a string with tolerance of 1 character mismatch

前端 未结 5 1618
天命终不由人
天命终不由人 2020-12-28 11:07

I was going through some Amazon interview questions on CareerCup.com, and I came across this interesting question which I haven\'t been able to figure out how to do. I have

5条回答
  •  感情败类
    2020-12-28 11:45

    Is the problem to do this efficiently?

    The naive solution is to loop over every substring of size substr in str, from left to right, and return true if the current substring if only one of the characters is different in a comparison.

    Let n = size of str Let m = size of substr

    There are O(n) substrings in str, and the matching step takes time O(m). Ergo, the naive solution runs in time O(n*m)

提交回复
热议问题