-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path03_sameCharacters.swift
68 lines (56 loc) · 2.05 KB
/
03_sameCharacters.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
// Do two strings contain the same characters?
// Write a function that accepts two String parameters, and returns
// true if they contain the same characters in any order.
func isSameCharacters(_ input1: String, _ input2: String) -> Bool {
let input1Array = Array(input1.characters)
var input2Array = Array(input2.characters)
// brute force - O(n^2)
for input1char in input1Array {
for input2char in input2Array {
// nested for loops check for matches
if input2char == input1char {
// remove the match
if let index = input2Array.index(of: input2char) {
input2Array.remove(at: index)
}
}
}
// if everything matched the array is empty - return true
if input2Array.count == 0 {
return true
}
}
// otherwise the strings did not match - return false
return false
}
// tighen up the check and return code
func isSameCharacters2(_ input: String, _ check: String) -> Bool {
let inputArray = Array(input.characters)
var checkArray = Array(check.characters)
// brute force - O(n^2)
for inputChar in inputArray {
// remove this character if its found in the check array
if let index = checkArray.index(of: inputChar) {
checkArray.remove(at: index)
}
}
return checkArray.count == 0
}
// sorted arrays - O(n log n)
func isSameCharactersSorted(_ input: String, _ check: String) -> Bool {
let inputArray = Array(input.characters)
let checkArray = Array(check.characters)
return inputArray.sorted() == checkArray.sorted()
}
print(isSameCharacters("hello", "world")) // false
print(isSameCharacters("hello", "hello")) // true
print(isSameCharacters2("hello", "world")) // false
print(isSameCharacters2("hello", "hello")) // true
print(isSameCharactersSorted("hello", "world")) // false
print(isSameCharactersSorted("hello", "hello")) // true
// Swift 4
func isSameCharactersSorted2(_ one: String, _ two: String) -> Bool {
return one.sorted() == two.sorted()
}
print(isSameCharactersSorted2("hello", "world")) // false
print(isSameCharactersSorted2("hello", "hello")) // true