Algorithms
Strings
This function is already included in Kotlin standard library
fun findFirstSubString(string: String, substring: String): Int? { if (string.isEmpty() || substring.isEmpty() || substring.length > string.length) return null val index = string.indexOf(substring) return if (index == -1) null else index }
fun findFirstSubString(string: String, subString: String): Int? {val stringArray = string.toList()val substringArray = subString.toList()if (stringArray.size < substringArray.size || stringArray.isEmpty() || substringArray.isEmpty()) {return null}var counter = 0var startIndex = 0var i = 0while (i < stringArray.size) {if (stringArray[i] == substringArray[counter]) {if(counter == 0) {startIndex = i}if ((counter + 1) == substringArray.size) {return startIndex}counter++i++}else {if (counter > 0) {i = startIndex + 1}else {i++}counter = 0startIndex = 0}println(i)}return null- fun findFirstSubString(string: String, substring: String): Int? {
- if (string.isEmpty() || substring.isEmpty() || substring.length > string.length) return null
- val index = string.indexOf(substring)
- return if (index == -1) null else index
- }