Sort by

recency

|

2393 Discussions

|

  • + 1 comment

    JAVA

     public static List<Integer> matchingStrings(List<String> stringList, List<String> queries) {
        // Write your code here
            List<Integer> result = new LinkedList<Integer>();
            
            for(int i = 0; i < queries.size(); i++){
                int count = 0;
                for (int j = 0; j < stringList.size(); j++){
                    if(queries.get(i).equalsIgnoreCase(stringList.get(j))){
                        count++;
                    }                
                }
                result.add(count);            
            }
            
            return result;
    
        }
    
  • + 0 comments

    Javascript using ES6:

    function matchingStrings(stringList, queries) {
        return queries.reduce((acc, query) => {
            let occurrences = 0;
            
            stringList.forEach(string => {
                if (string === query) {
                    occurrences++;
                }
            });
            
            acc.push(occurrences);
            
            return acc;
        }, []);
    }
    
  • + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM, replicateM_)
    import qualified Data.Map as M
    
    main :: IO ()
    main = do
        n <- readLn :: IO Int
        ds <- replicateM n getLine
        let dm = M.fromListWith (+) $ map (\s -> (s,1)) ds
        q <- readLn :: IO Int
        replicateM_ q $ do
            s <- getLine
            print $ M.findWithDefault 0 s dm
            
    
  • + 0 comments

    java

    Map<String, Integer> m = new HashMap<String, Integer>();
    for (int i = 0; i < stringList.size(); i++) {
    		String val = stringList.get(i);
    		if (m.containsKey(val)) {
    				m.put(val, m.get(val) + 1);
    		} else {
    				m.put(val, 1);
    		}
    }
    
    List<Integer> res = new ArrayList<Integer>(queries.size());
    for (int j = 0; j < queries.size(); j++) {
    		Integer newVal = m.getOrDefault(queries.get(j), 0);
    		res.add(newVal);
    }
            
            return res;
    
  • + 0 comments

    Creating a defaultdict makes this one pretty easy.

    ` from collections import defaultdict

    def matchingStrings(stringList, queries): hm = defaultdict(int) for key in stringList: hm[key] += 1 return [hm[key] for key in queries] `