-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
771 Jewels and Stones.swift
40 lines (34 loc) 路 1.02 KB
/
771 Jewels and Stones.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
//
// 771 Jewels and Stones.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 05/04/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/jewels-and-stones
class Solution {
/// Return how many of your stones are also jewels.
///
/// - Parameters:
/// - J: Representing the types of stones that are jewels.
/// - S: Representing the stones you have.
///
/// - Returns: How many of your stones are jewels.
///
/// - Complexity:
/// - time: O(j + s), where j is the length of the J, and s is the length of the S.
/// - space: O(j), where j is the length of the J.
func numJewelsInStones(_ J: String, _ S: String) -> Int {
var jewels = Set<Character>()
for jewel in J {
jewels.insert(jewel)
}
var ans = 0
for stone in S {
guard jewels.contains(stone) else { continue }
ans += 1
}
return ans
}
}