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
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)