Skip to content

Instantly share code, notes, and snippets.

@spurscho
Last active December 18, 2019 12:49
Show Gist options
  • Save spurscho/3569448aabc4dee453826e1da9478ad5 to your computer and use it in GitHub Desktop.
Save spurscho/3569448aabc4dee453826e1da9478ad5 to your computer and use it in GitHub Desktop.
Leetcode 3. Longest Substring Without Repeating Characters (Time limit exceeded)
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