jewels-and-stones.dart (688B)
1 //For each stone check if the given index is the same character as 2 //a jewel. If it is add one to the number of jewels returned. 3 //This algorithm is a O(n + m) time complexity where n is the length of the stones and m is the length of the jewels. 4 //Runtime: 249ms Beats: 83.33% 5 //Memory: 141MB Beats: 40.48% 6 class Solution { 7 int numJewelsInStones(String jewels, String stones) { 8 Set <String> vals = {}; 9 for(int i = 0 ; i < jewels.length ; ++i){ 10 vals.add(jewels[i]); 11 } 12 int return_val = 0; 13 for(int i = 0 ; i < stones.length ; ++i){ 14 if(jewels.contains(stones[i])){ 15 return_val += 1; 16 } 17 } 18 return return_val; 19 } 20 }