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
- Data Structures
- Arrays
- Sparse Arrays
- Discussions
Sparse Arrays
Sparse Arrays
+ 0 comments Here's my solution following the best possible Big O notation:
function SparseArray(S, Q) { let ob = {} let result = [] for (let i = 0; i < S.length; i++) { ob[S[i]] = (ob[S[i]] || 0) + 1; } Q.forEach(e => { result.push(ob[e] || 0); }); return result; }
+ 0 comments Simple and Clean 3 Line Solution
public static List<Integer> matchingStrings(List<String> stringList, List<String> queries) { // Write your code here List<Integer> list = new ArrayList<>(); for(String q : queries){ list.add(Collections.frequency(stringList, q)); } return list; }
+ 0 comments Python solution for O(n+m) time complexity
def matchingStrings(stringList, queries): d = {} c = [] for i in stringList: d[i] = 0 for a in stringList: if a in d.keys(): d[a] += 1 for b in queries: if b in d.keys(): c.append(d.get(b)) else: c.append(0) return c
+ 0 comments Python solution
def matchingStrings(stringList, queries): # Write your code here count=[] for i in range(len(queries)): count.append(stringList.count(queries[i])) return count
+ 0 comments Here is my javascript solution in liniear time complexity.
function matchingStrings(stringList, queries) { // Write your code here const obj = {}; for(let i = 0; i < stringList.length; i++) { obj[stringList[i]] = (obj[stringList[i]] || 0) + 1; } let result = [] for (var i = 0; i < queries.length; i++) { result.push(obj[queries[i]] || 0) } return result }
Load more conversations
Sort 2180 Discussions, By:
Please Login in order to post a comment