Ad
Algorithms
Strings

This function is already included in Kotlin standard library

Code
Diff
  • 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 = 0
    • var startIndex = 0
    • var i = 0
    • while (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 = 0
    • startIndex = 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
    • }
Code
Diff
  • fun returnInt(int: Int) = int
    • fun returnInt(int:Int): Int{
    • return int
    • }
    • fun returnInt(int: Int) = int