-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL771.java
More file actions
38 lines (38 loc) · 1.36 KB
/
L771.java
File metadata and controls
38 lines (38 loc) · 1.36 KB
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
class Solution771 {
class Solution {
/**
* 771. Jewels and Stones https://leetcode.com/problems/jewels-and-stones/description/
*
* @param J Jewel string
* @param S Stone string
* @return Number of stones which are jewels
* @timeComplexity O(max ( j, s)) where j is number of jewels and s is number of stones
* @spaceComplexity O(1) Using a boolean array of fixed size i.e. 52
*/
public int numJewelsInStones(String J, String S) {
boolean[] isJewel = new boolean[52];
for (char c : J.toCharArray()) {
if (c <= 'Z') {
// We record whether a capital letter is jewel from 0-25
isJewel[c - 'A'] = true;
} else if (c <= 'z') {
// We record whether a small letter is jewel from 26-51
isJewel[c - 'a' + 26] = true;
}
}
int count = 0;
for (char c : S.toCharArray()) {
if (c <= 'Z') {
if (isJewel[c - 'A']) {
count++;
}
} else if (c <= 'z') {
if (isJewel[c - 'a' + 26]) {
count++;
}
}
}
return count;
}
}
}