Find 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 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

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions