You're given strings J
representing the types of stones that are jewels, and S
representing the stones you have. Each character in S
is a type of stone you have. You want to know how many of the stones you have are also jewels.
The letters in J
are guaranteed distinct, and all characters in J
and S
are letters. Letters are case sensitive, so "a"
is considered a different type of stone from "A"
.
Example 1:
Input: J = "aA", S = "aAAbbbb"
Output: 3
Example 2:
Input: J = "z", S = "ZZ"
Output: 0
Note:
S
andJ
will consist of letters and have length at most 50.- The characters in
J
are distinct.
---------------------------------------------------------------------------------------------------------------------------
Approach #1: Brute Force 两个for循环搞定
class Solution {
public int numJewelsInStones(String J, String S){
int count = 0;
for (char s: S.toCharArray())
for (char j: J.toCharArray())
if(j == s){
count+=1;
break;
}
return count;
}
}
Complexity:
Time: O(J.length * S.length)
Space: O(J.length * S.length), because of the creation of new arrays.(in Java, Strings cannot be modified.)
知识点:
1. Java对String的处理,使用String.toCharArray(),可以把String转化为一个char的数组。
2. 记住这里这种for循环的写法。
Approach #2: Hash Set
class Solution {
public int numJewelsInStones(String J, String S) {
Set<Character> Jset = new HashSet(); // declare a Set<element>
for (char j: J.toCharArray())
Jset.add(j); // add elements to Set
int ans = 0;
for (char s: S.toCharArray())
if (Jset.contains(s)) // Object.contains()
ans++;
return ans;
}
}
Complexity:
Time: O(J.length+S.length), there are two seperate for loops.
Space: O(J.length), because we created a Set object of J.length.
知识点:
1. HashSet的声明方法及Set的一些method的使用,Object.add(), Object.contains().
boolean add(E e)
Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.
The stipulation above does not imply that sets must accept all elements; sets may refuse to add any particular element, including null, and throw an exception, as described in the specification for Collection.add
. Individual set implementations should clearly document any restrictions on the elements that they may contain.
Specified by:
add
in interface Collection<E>
Parameters:
e
- element to be added to this set
Returns:
true if this set did not already contain the specified element
Throws:
UnsupportedOperationException
- if the add operation is not supported by this set
ClassCastException
- if the class of the specified element prevents it from being added to this set
NullPointerException
- if the specified element is null and this set does not permit null elements
IllegalArgumentException
- if some property of the specified element prevents it from being added to this set
boolean contains(Object o)
Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).
Specified by:
contains
in interface Collection<E>
Parameters:
o
- element whose presence in this set is to be tested
Returns:
true if this set contains the specified element
Throws:
ClassCastException
- if the type of the specified element is incompatible with this set (optional)
NullPointerException
- if the specified element is null and this set does not permit null elements (optional)
boolean remove(Object o)
Removes the specified element from this set if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)
Specified by:
remove
in interface Collection<E>
Parameters:
o
- object to be removed from this set, if present
Returns:
true if this set contained the specified element
Throws:
ClassCastException
- if the type of the specified element is incompatible with this set (optional)
NullPointerException
- if the specified element is null and this set does not permit null elements (optional)
UnsupportedOperationException
- if the remove operation is not supported by this set