-
Notifications
You must be signed in to change notification settings - Fork 15
/
389_FindTheDifference.swift
55 lines (51 loc) · 1.12 KB
/
389_FindTheDifference.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
//
// 389_FindTheDifference.swift
// LeetcodeSwift
//
// Created by yansong li on 2016-09-05.
// Copyright © 2016 YANSONG LI. All rights reserved.
//
import Foundation
/**
Title:389 Find the difference
URL: https://leetcode.com/problems/find-the-difference/
Space: O(n)
Time: O(n)
*/
class FindTheDifference_Solution {
func solution(_ s: String, t: String) -> Character {
var sDict: [Character : Int] = [:]
var tDict: [Character : Int] = [:]
for sc in s.characters {
if let sCount = sDict[sc] {
sDict[sc] = sCount + 1
} else {
sDict[sc] = 1
}
}
for tc in t.characters {
if let tCount = tDict[tc] {
tDict[tc] = tCount + 1
} else {
tDict[tc] = 1
}
}
for (c, _) in tDict {
if sDict[c] != nil {
continue
} else {
return c
}
}
return "*"
}
func test() {
let result = solution("abcd", t: "abcde")
let correctResult = "e"
if result == correctResult[correctResult.startIndex] {
print("Find the difference Pass")
} else {
print("Find the difference Failed")
}
}
}