zoukankan      html  css  js  c++  java
  • 771. Jewels and Stones

    You're given strings J representing the types of stones that are jewels, and Srepresenting 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 and J will consist of letters and have length at most 50.
    • The characters in J are distinct.

    time: O(M+N), space: O(M)  -- M: # of length of J

    class Solution {
        public int numJewelsInStones(String J, String S) {
            Set<Character> set = new HashSet<>();
            for(char c : J.toCharArray()) {
                set.add(c);
            }
            int cnt = 0;
            for(char c : S.toCharArray()) {
                if(set.contains(c)) {
                    cnt++;
                }
            }
            return cnt;
        }
    }
  • 相关阅读:
    Feature fake , new view in comment.
    MeeGo Architect
    小米手机 怪诞行为经济学
    [转载]ten years as a programmer
    C++ 头文件
    关于AGILE/TDD 和传统的design
    你是否在开发正确的产品
    正确的创业
    MeeGo架构
    Unit Test
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10209714.html
Copyright © 2011-2022 走看看