File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun minWindow (s : String , t : String ): String {
3+ if (s.length < t.length) {
4+ return " "
5+ }
6+ val containIndexList = mutableListOf<Int >()
7+ for (i in s.indices) {
8+ if (t.contains(s[i])) {
9+ containIndexList.add(i)
10+ }
11+ }
12+ var answer = " "
13+ val regex =
14+ t.toCharArray().joinToString(separator = " " , prefix = " ^" , postfix = " .+$" ) { """ (?=.*${it} )""" }.toRegex()
15+ for (i in 0 .. containIndexList.size - t.length) {
16+ val startIndex = containIndexList[i]
17+ for (l in t.length.. containIndexList.size - i) {
18+ val endIndex = containIndexList[(i + l) - 1 ] + 1
19+ val subStr = s.substring(startIndex, endIndex)
20+ if (regex.containsMatchIn(subStr)) {
21+ if (answer.isEmpty()) {
22+ answer = subStr
23+ } else if (subStr.length < answer.length) {
24+ answer = subStr
25+ }
26+ break
27+ }
28+ }
29+ }
30+ return answer
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments