Created
August 8, 2020 02:04
-
-
Save kimji1/689afe2378456092eb0f65a4b886efbf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| println(solution("azABaabza")) | |
| println(solution("ABcabbCa")) | |
| println(solution("azABaabza")) | |
| println(solution("CATattac")) | |
| println(solution("TacoCat")) | |
| println(solution("Madam")) | |
| println(solution("AcZCbaBz")) | |
| println(solution("aZABcabbCa")) | |
| } | |
| fun solution(S: String): Int { | |
| for (k in 1..S.length) { | |
| for (i in 0 until S.length - k + 1) { | |
| val lowerSet = mutableSetOf<Char>() | |
| val upperSet = mutableSetOf<Char>() | |
| val temp = S.substring(i, i + k) | |
| for (ch in temp) { | |
| if (ch.isLowerCase()) lowerSet.add(ch) else upperSet.add(ch) | |
| } | |
| if (containsAllElements(lowerSet, upperSet) && containsAllElements(upperSet, lowerSet)) { | |
| return temp.length | |
| } | |
| } | |
| } | |
| return -1 | |
| } | |
| fun containsAllElements(first: Set<Char>, second: Set<Char>): Boolean { | |
| val firstLower = first.map { it.toLowerCase() } | |
| val secondLower = second.map { it.toLowerCase() } | |
| return firstLower.containsAll(secondLower) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment