I, entitled 1
J given string represents the type of gem stones, and S represents a string of stone you have. S Each character represents a type of stone you have, you want to know the stone you have in how many gems.
J letters will not be repeated, J S and all the characters are letters. A case-sensitive, so "a" and "A" are different types of stones.
Example 1:
Input: J = "aA", S = "aAAbbbb"
Output: 3
Example 2:
Input: J = "z", S = "ZZ"
Output: 0
note:
- J S and containing up to 50 letters.
- J The characters are not repeated.
Second, the idea
Third, the code
public class T0771 {
public static void main(String[] args) {
System.out.println( numJewelsInStones("aA","aAAbbbb" ) ); //3
System.out.println( numJewelsInStones("z","ZZ" ) ); //0
}
public static int numJewelsInStones(String J, String S) {
//最后的输出结果
int result = 0;
//将每一个判断项目转换为数组
char[] item = J.toCharArray();
//遍历字符串S输出结果
for( int i = 0; i < S.length(); i++ ){
for( char s : item ){
if( s == S.charAt(i) )
result++;
}
}
return result;
}
}
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/jewels-and-stones
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source. ↩︎