LeetCode #771.Jewels and Stones python

Question

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".

Example1:

Input: J = "aA", S = "aAAbbbb"
Output: 3

Example2:

Input: J = "z", S = "ZZ"
Output: 0

Thought(思路)

It is easy to do. I will talk about thought and data structure.(思路很简单,大致说一下,主要说一下可能涉及到的数据结构。)
J include unrepeated elements. Let us loop J and check whether everyone of J is included S and sum the counts.(J是不会重复的,循环J中所有元素,看是否在S中,在,就统计个数。)

Data structure

Array, Hashtable/Hashmap, Tree(Linklist).
In python3, Accessing set is faster than accessing list.(在python3中,访问集合比访问列表速度快)
How to use map().
map function takes an arbitrary function and applies it over the elements of an iterable. An iterable, loosely speaking, is anything which allows you to loop through its elements.
Consider this example,

>>> round(2.3)
2
>>> round(4.9)
5
>>> rounded = map(round, [2.3, 4.9])
>>> list(rounded)
[2, 5]

There are two main components to this example:

  • the round function
  • the iterable i.e., list [2.3, 4.9]

Let’s say you have this list of hundred numbers that you want to round off. Of course, you are not going to write hundred round statements. One of the many ways to achieve this [in Python] is to use map.

You can provide map with the function as argument, and the list of elements on which it has to be applied. The result is a generator expression. The last step retrieves the results from the generator.


map

Now, let’s look at the code below:

a, b = map(int, input().split())

Here’s a breakdown:

In [0]: input().split()
12 13
Out [0]: ['12', '13']

That is, take an input, split it and create a list, then convert them into an integer by mapping the function int, finally save the results in variables a, b.

That’s one way you can take multiple inputs on the same line.
source from: https://www.quora.com/What-does-a-map-function-do-in-Python-3

Code

#the First solution
class Solution:
    def numJewelsInStones(self, J, S):
        """
        :type J: str
        :type S: str
        :rtype: int
        """
        j = set(list(J))
        s = list(S)
        count = 0
        for stone in s:
            if stone in j:
                count += 1
        return count
#the Second
class solution:
    def numJewelsInStones(self,J,S):
        return sum(s in J for s in S)
#the Third
class solution:
     def numJewelsInStones(self, J, S):
         return sum(map(J.count, S))
#the Fourth
class solution:
      def numJewelsInStones(self, J, S):
          return sum(map(S.count, J))  

Complexity

Time complexity: O(s*j)(s is length of S,j is length of J)(为字符串S的长度,j为字符串J的长度)
Space complexity: O(1)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 五分钟商学院一年课程结束了,事实上我深深地感觉到自己很多概念没吃透,错过太多知识点。 离下一季开课还有一段时间,立...
    身兼数职老板娘阅读 370评论 0 1
  • 默 我曾与时光赛跑, 我的世界都是光。 有一天你闯入我的世界,看着我, 你眼中的星空,让我不知...
    半部小说阅读 180评论 0 2
  • 虽然现在整天敲代码敲到凌晨累得不想动,但是当你敲的有灵感时,你会感觉很充实的,那种成果最终显现在自己面前,心里是...
    lp79阅读 199评论 0 1
  • 本文摘要:今天是《郑渊洁的家庭教育课》的第十八课,我们为大家继续解读。本文深度剖析了大众教育中从众的特性,过渡到家...
    石头聊家庭教育阅读 1,475评论 11 12