-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
389 Find the Difference.swift
39 lines (33 loc) 路 996 Bytes
/
389 Find the Difference.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
//
// 389 Find the Difference.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 24/09/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/find-the-difference/
class Solution {
/// Finds the letter that appears just in `t`.
///
/// - Parameters:
/// - s: The string.
/// - t: The string.
/// - Returns: The letter that appears just in `t`.
///
/// - Complexity:
/// - time: O(n), where n is the length of `t`.
/// - space: O(1), only constant space is used. The total number of unique characters
/// is 26.
func findTheDifference(_ s: String, _ t: String) -> Character {
var dict = [Character: Int]()
for ch in s {
dict[ch, default: 0] += 1
}
for ch in t {
guard let num = dict[ch], num > 0 else { return ch }
dict[ch] = num - 1
}
return "\0"
}
}