数据结构与算法:leetcode_771_Jewels and Stones

  • Description
    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”.

    S and J will consist of letters and have length at most 50.
    The characters in J are distinct.

  • Example
    Input: J = “aA”, S = “aAAbbbb”
    Output: 3
    Input: J = “z”, S = “ZZ”
    Output: 0
  • Solution
public static int stonesInJewels(String jewels,String stones) {
        int slength = stones.length();
        int numbers = 0;
        for(int i = 0; i< slength;i++) {
            if(jewels.contains(stones.subSequence(i, i+1))){
                numbers++;
            }
        }
        System.out.println("the numbers is : "+numbers);
        return numbers;
    }

猜你喜欢

转载自blog.csdn.net/weixin_38021928/article/details/81149957