use std::collections::HashSet;fn f(j:&str,s:&str)->usize{let x:HashSet<char>=j.chars().collect();s.chars().filter(|c|x.contains(c)).count()}fn main(){println!("{}",f("aA","aAAbbbb"));}
Solution 2 - One-Liner with sum()
Use a generator expression with sum() to count stones that appear in the jewels set. Same logic as Solution 1, but written more concisely.
TimeComplexity:O(j+s)
SpaceComplexity:O(j)
function jewels(j,s){let x=new Set(j),n=0;for(const c of s)if(x.has(c))n++;return n}console.log(jewels("aA","aAAbbbb"));
function jewels(j,s){let x=new Set(j),n=0;for(const c of s)if(x.has(c))n++;return n}console.log(jewels("aA","aAAbbbb"));
use std::collections::HashSet;fn f(j:&str,s:&str)->usize{let x:HashSet<char>=j.chars().collect();s.chars().filter(|c|x.contains(c)).count()}fn main(){println!("{}",f("aA","aAAbbbb"));}
""" 0771.1 - Solution 1 - Hash Set Lookup """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def numJewelsInStones(self, jewels: str, stones: str) -> int:
"""Jewels and Stones Function"""
jewel_set = set(jewels)
count = 0
for stone in stones:
if stone in jewel_set:
count += 1
return count
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().numJewelsInStones("aA", "aAAbbbb")) # 3
print(Solution().numJewelsInStones("z", "ZZ")) # 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0771.2 - Solution 2 - One-Liner with sum() """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def numJewelsInStones(self, jewels: str, stones: str) -> int:
"""Jewels and Stones Function"""
return sum(s in set(jewels) for s in stones)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().numJewelsInStones("aA", "aAAbbbb")) # 3
print(Solution().numJewelsInStones("z", "ZZ")) # 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()