1. What I learned
a. for e in ‘String’
When I use “for e in ‘String’”, e means each character in ‘String’.
ex)
for e in 'Hello'
print (e)
–> H,e,l,l,o (actually, each character is printed on each line)
b. count() method
This method counts ‘keyword’ in ‘target’.
ex) ‘target’.count(‘keyword’)
cf) I can use either ‘ ‘ or “ “
c. map() method
This method executes ‘function’ in ‘target’.
ex) map(‘function’, ‘target’)
c. sum() method
This adds everything in ().
ex)
list = [1,2,3]
print (sum(list))
–> 6 is printed.
2. How I sloved
I have to count the number of each character of J in S. So I use a ‘for loop’. The number of repetitions of ‘for loop’ was the number of characters in J. And for each loop, the frequency of each charater in S was added.
3. Code
class Solution:
def numJewelsInStones(self, J: str, S: str) -> int:
numOfJew = 0
for e in J:
numOfJew += S.count(e)
return numOfJew
class Solution:
def numJewelsInStones(self, J: str, S: str) -> int:
return sum(map(J.count, S))
4. Result
Runtime : 24 ms(88.17%), Memory usage : 12.7 MB(100.00%)
(Runtime can be different by a system even if it is a same code.)