-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeet459.swift
More file actions
36 lines (31 loc) · 898 Bytes
/
Leet459.swift
File metadata and controls
36 lines (31 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Foundation
func repeatedSubstringPattern_정답(_ s: String) -> Bool {
var tempS = s + s
tempS.removeFirst()
tempS.removeLast()
return tempS.contains(s)
}
func repeatedSubstringPattern_삽질_1(_ s: String) -> Bool {
let arrayS = s.map { $0 }
var j: Int = 0
var pattern: String = ""
var startIndex: Int = -1
for i in 1..<s.count {
if j == startIndex { break }
if arrayS[i] == arrayS[j] {
if pattern == "" { startIndex = i }
pattern.append(arrayS[i])
j += 1
} else {
pattern = ""
j = 0
}
}
return s.replacingOccurrences(of: pattern, with: "") == ""
}
func repeatedSubstringPattern_삽질_2(_ s: String) -> Bool {
if s.count % 2 != 0 { return false }
let arrayS = s.map { $0 }
let pattern = String(arrayS[0..<arrayS.count/2])
return pattern+pattern == s
}