Skip to content

Instantly share code, notes, and snippets.

@bng86
Created September 12, 2021 13:23
Show Gist options
  • Save bng86/55f6d83403eafa2dddba78a2e2b0d90b to your computer and use it in GitHub Desktop.
Save bng86/55f6d83403eafa2dddba78a2e2b0d90b to your computer and use it in GitHub Desktop.
class Solution {
fun strStr(source: String, target: String): Int {
if (target.isEmpty()) {
return 0
}
if(source == target) {
return 0
}
var i = 0
var j = 0
var result = -1
while (source.lastIndex >= i) {
if (j == target.length) {
return result
}
val c: Char = source[i]
if (c == target[j]) {
if (j == 0) {
result = i
}
j++
} else {
if (j != 0) {
i = result
}
j = 0
}
i += 1
}
return if (j == target.length) {
result
} else {
-1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment