We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Strings
- Gemstones
- Discussions
Gemstones
Gemstones
+ 0 comments Python:
def gemstones(arr): s = set(arr.pop()) for string in arr: s = s.intersection(string) return len(s)
+ 0 comments Python, using reduce
import reduce from functools def gemstones(arr): sarr = list(map(set, arr)) return len(reduce(lambda c, f: c.intersection(f), sarr))
+ 0 comments Here are my c++ approaches of solving this problem, video explanation here : https://youtu.be/EjhdJXN3a3c
Solution 1 : Using a list of all possible characters
int gemstones(vector<string> arr) { int result = 0; string liste = "abcdefghijklmnopqrstuvwxyz"; for(int j = 0; j < liste.size(); j++){ int cp = 0; for(int i = 0; i < arr.size(); i++){ if(arr[i].find(liste[j]) != string::npos) cp++; } if(cp == arr.size())result++; } return result; }
Solution 2 : Using set of the shortest element of the array of string
string getShortest(vector<string> arr) { string result = arr[0]; for(int i = 1; i < arr.size(); i++){ if(arr[i].size() < result.size()) result = arr[i]; } return result; } int gemstones(vector<string> arr) { int result = 0; string shortest = getShortest(arr); set<char> liste(shortest.begin(), shortest.end()); for(auto j = liste.begin(); j != liste.end(); j++){ int cp = 0; for(int i = 0; i < arr.size(); i++){ if(arr[i].find(*j) != string::npos) cp++; } if(cp == arr.size())result++; } return result; }
+ 0 comments Python 3
def gemstones(arr): dict_ans = {} counter = 0 for i in arr: for j in set(i): dict_ans[j] = (dict_ans.get(j) or 0) + 1 if dict_ans[j] == len(arr): counter += 1 return counter
+ 0 comments javaScript:
function gemstones(arr) { // Write your code here let count=0; let minerals="" let str=arr.join(""); for(let i of str){ if(!minerals.includes(i)){ minerals+=i } } for (let i of minerals){ let skip=false for(let j of arr){ if(!j.includes(i)){ skip=true break; } } if(skip==false){ count++ } } return count }
Load more conversations
Sort 951 Discussions, By:
Please Login in order to post a comment