I'll apologize now because this questions sounds stupid in my head and I am probably overlooking something very glaringly obvious. Anyway...
Okay so I am teaching myself scala and as a learning exercise I decided to implement a method that determines if one string contains another, smaller string. The first thing I did was use the naive version where I go to each letter of the string and start checking forward to see if each character matches. That went fine. Then I decided to implement a more efficient method and this is what I came up with (no special cases included):
// return true if a is a substring of b
def is_sub(a: String, b: String) : Boolean = {
for(i <- 0 until b.length-a.length) { // O(n-m)
if(a.hashCode == b.substring(i,a.length+i).hashCode) return true // O(1) + O(1) + O(1)
}
return false
}
First, can someone check my work and make sure I am right. I am prone to stupid mistakes. Second, can you check to make sure my time complexities are accurate. Assuming the first 2 are what I think they are why isn't this method mentioned on the wikipedia page for string searching algorithms? In theory this should be O(n-m) with no preprocessing space needed.
Where have I screwed up my analysis of this problem?