Last active
December 18, 2019 12:49
-
-
Save spurscho/3569448aabc4dee453826e1da9478ad5 to your computer and use it in GitHub Desktop.
Leetcode 3. Longest Substring Without Repeating Characters (Time limit exceeded)
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
class Solution { | |
func lengthOfLongestSubstring(_ s: String) -> Int { | |
guard s != "" else { | |
return 0 | |
} | |
let arr = Array(s) | |
var resStr: String = "\(arr[0])" | |
var tempStr: String = "\(arr[0])" | |
for i in 0 ..< arr.count-1 { | |
tempStr = "\(arr[i])" | |
for j in i+1 ..< arr.count { | |
if tempStr.firstIndex(of: arr[j]) == nil { // 다른 문자 | |
tempStr += "\(arr[j])" | |
if tempStr.count > resStr.count { | |
resStr = tempStr | |
} | |
}else{ | |
break | |
} | |
} | |
} | |
print("The answer is \"\(resStr)\", with the length of \(resStr.count).") | |
return resStr.count | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment