Leetcode #771. Jewels and Stones

You‘re given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in Sis 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

题意:给出两个字符串,一个是要匹配的模式串,一个是要匹配的字符串,模式串每个字母都是要匹配的字母,统计目标串中模式串各个字母出现的次数。

想法:最粗暴的想法就是去把模式串中的字母存成像Map或者List的结构数据,然后用的时候去依次扫,如果考虑找是O(1)的话就直接是O(N),(模式串长M,目标串N),如果找是O(M),就是O(MN),目测map的查找应该是O($\sqrt(M)$).后面我的做法是先分别排序,然后合并查找,利用的是两者依然有序的特点。具体可以参考下下面的代码,我看了用一行的做法,是利用了python本身map的用法,map(func, iterable),把func依次作用到iterable的元素上,值得注意的是list.count(str)这个函数,是去匹配list里面出现了多少个str。

我的代码实现:

class Solution(object):
    def numJewelsInStones(self, J, S):
        """
        :type J: str
        :type S: str
        :rtype: int
        """
        target = sorted(J)
        source = sorted(S)
        i = len(target)
        j = len(source)
        index1 = 0
        index2 = 0
        cnt = 0
        while index1 < j and index2 < i:
            if source[index1] < target[index2]:
                index1+=1
            elif source[index1] == target[index2]:
                index1+=1
                cnt+=1
            else:
                index2+=1
        return cnt

  

一行的实现:

def numJewelsInStones(self, J, S):
    return sum(map(J.count, S))

  

原文地址:https://www.cnblogs.com/lhppom/p/8475488.html

时间: 2024-08-30 14:24:41

Leetcode #771. Jewels and Stones的相关文章

【博客搬家旧文】leetcode 771. Jewels and Stones

今天开通了博客园 ,之前的博客就不用了.之后再陆陆续续把之前的博文重新放到这里来.有标题这个tag的都是搬运的旧博客的文章.希望在这里是个新的开始,嘻嘻. import java.util.Scanner; class Solution { public static int numJewelsInStones(String J, String S) { int count=0; for(int i=0; i<J.length(); i++){ for(int j=0; j<S.length(

[LeetCode&amp;Python] Problem 771: Jewels and Stones

You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in Sis 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

771. Jewels and Stones 珠宝和石头

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

771. Jewels and Stones - Easy

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

letCode(771 Jewels and Stones )

问题描述: 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

codechef Jewels and Stones 题解

Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery accessories. She has been collecting stones since her childhood - now she has become really good with identifying which ones are fake and which ones are no

leetCode题解之Jewels and Stones

1.题目描述 2.分析 使用HashTable 是解决这种的好方法. 3.代码 1 int numJewelsInStones(string J, string S) { 2 map<char,int> m; 3 for(char c : S) 4 m[c]++; 5 6 int ans = 0; 7 for( char c: J) 8 ans += m[c]; 9 return ans; 10 11 } 原文地址:https://www.cnblogs.com/wangxiaoyong/p/

Jewels and Stones

题目描述: You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in Sis a type of stone you have.  You want to know how many of the stones you have are also jewels. The letters in 

[Swift]LeetCode771. 宝石与石头 | Jewels and Stones

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