![JAR search and dependency download from the Maven repository](/logo.png)
g1701_1800.s1763_longest_nice_substring.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1701_1800.s1763_longest_nice_substring
// #Easy #String #Hash_Table #Bit_Manipulation #Sliding_Window
// #2023_06_18_Time_158_ms_(91.67%)_Space_36.1_MB_(91.67%)
class Solution {
fun longestNiceSubstring(s: String): String {
val index = isNotNiceString(s)
if (index == -1) {
return s
}
val left = longestNiceSubstring(s.substring(0, index))
val right = longestNiceSubstring(s.substring(index + 1))
return if (left.length >= right.length) left else right
}
private fun isNotNiceString(s: String): Int {
val set: MutableSet = HashSet()
for (c in s.toCharArray()) {
set.add(c)
}
for (i in 0 until s.length) {
val c = s[i]
if (!set.contains(c.lowercaseChar()) ||
!set.contains(c.uppercaseChar())
) {
return i
}
}
return -1
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy