-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path08_isRotate.swift
71 lines (56 loc) · 2.37 KB
/
08_isRotate.swift
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Challenge 8: String is rotated
// Write a function that accepts two strings, and returns true if
// one string is a rotation of the other.
import Foundation
// classic brute force solution
func isRotate(string1: String, string2:String) -> Bool {
let letter = Array(string1.characters)[0]
var string2_index = 0
// short circuit if the two strings are not of equal length
if string1.characters.count != string2.characters.count {
return false
}
for match in string2.characters {
if letter == match {
// make a new string out of the slice before the match and compare
let newIndex = string2.index(string2.startIndex, offsetBy: string2_index)
let prefix = string2.substring(to: newIndex)
let suffix = string2.substring(from: newIndex)
let potential_Rotate = "\(suffix)\(prefix)"
if string1 == potential_Rotate {
return true
}
}
string2_index += 1 // increment the index to string 2
}
return false
}
print("-------------------")
print(isRotate(string1: "Swift", string2: "ftSwi")) // true
print(isRotate(string1: "tca", string2: "cat")) // true
print(isRotate(string1: "atc", string2: "cat")) // true
print(isRotate(string1: "dog", string2: "catty")) // false
// -------------------------------------------------------------
// Alternate solution which doubles the second string
// and if the first is in the second and they are the same length
// then its a rotation.
func isRotateDoubler(_ input: String, _ rotated: String) -> Bool {
guard input.characters.count == rotated.characters.count else { return false }
let doubledUp = rotated + rotated
return doubledUp.contains(input)
}
print("-------------------")
print(isRotateDoubler("Swift", "ftSwi")) // true
print(isRotateDoubler("tca", "cat")) // true
print(isRotateDoubler("atc", "cat")) // true
print(isRotateDoubler("dog", "catty")) // false
//Swift 4
func isRotateDoubler2(_ strOne: String, _ strTwo: String) -> Bool {
guard strOne.count == strTwo.count else { return false }
return (strTwo+strTwo).contains(strOne)
}
print("-------------------")
print(isRotateDoubler2("Swift", "ftSwi")) // true
print(isRotateDoubler2("tca", "cat")) // true
print(isRotateDoubler2("atc", "cat")) // true
print(isRotateDoubler2("dog", "catty")) // false