Kotlin program to find the longest substring without repeating characters

Given a string, find the longest substrings without repeating characters. Iterate through the given string, find the longest maximum substrings.

Kotlin program to find the longest substring without repeating characters

import java.util.HashSet

fun subString(input: String): String {

    val set = HashSet()

    var longestOverAll = ""
    var longestTillNow = ""

    for (i in 0 until input.length) {
        val c = input[i]

        if (set.contains(c)) {
            longestTillNow = ""
            set.clear()
        }
        longestTillNow += c
        set.add(c)
        if (longestTillNow.length > longestOverAll.length) {
            longestOverAll = longestTillNow
        }
    }

    return longestOverAll
}

fun main(args: Array) {
    val input = "substringfindout"
    println(subString(input))
}


Output

stringf
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions