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.
My Java solution with linear time complexity and linear space complexity:
publicstaticintbeautifulPairs(List<Integer>A,List<Integer>B){// create a frequency map to get the count of eacg valMap<Integer,Integer>freqMap=newHashMap<>();for(intval:A)freqMap.put(val,freqMap.getOrDefault(val,0)+1);//iterate over each val in b and accoridngly increment the amt of pairsintpairs=0;for(intval:B){if(freqMap.containsKey(val)&&freqMap.get(val)>0){pairs++;freqMap.put(val,freqMap.getOrDefault(val,0)-1);}}//if amt of pairs == a.size(), decrease it because by 1 bc an element must be changed//else add 1 bc an element isnt pairedreturn(pairs==A.size())?pairs-1:pairs+1;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Beautiful Pairs
You are viewing a single comment's thread. Return to all comments →
My Java solution with linear time complexity and linear space complexity: