The main point to keep in mind with this one is that all values in b must be accounted for, as we see with the test case with 36100 as a value. Additionally, we can't use a set for this problem since values may show up in either a or b multiple times, so that's out. So instead, we can calculate the frequency of occurrences of values instead, which leads to this O(a + b) solution:
#!/usr/bin/env python3
defcalc_frequency(l):freqs={}fornuminl:freqs[num]=freqs.get(num,0)+1returnfreqsdefcomp(a,b):ifaisNoneorbisNone:# early exit on null arrays
returnFalseb_freqs=calc_frequency(b)fornumina:square=num**2ifsquarenotinb_freqs:# early exit if the square of some value in a simply isn't in b
returnFalseb_freqs[square]-=1forfreqinb_freqs.values():iffreq!=0:# could be < 0 if a has more occurrences, or > 0 if b has more occurrences
returnFalsereturnTrueif__name__=="__main__":print(comp([121,144,19,161,19,144,19,11],[121,14641,20736,361,25921,361,20736,361]))print(comp([121,144,19,161,19,144,19,11],[11*11,121*121,144*144,19*19,161*161,19*19,144*144,19*19]))print(comp([121,144,19,161,19,144,19,11],[132,14641,20736,361,25921,361,20736,361]))print(comp([121,144,19,161,19,144,19,11],[121,14641,20736,36100,25921,361,20736,361]))print(comp([],[]))print(comp([1,2,3],None))
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The main point to keep in mind with this one is that all values in
b
must be accounted for, as we see with the test case with36100
as a value. Additionally, we can't use a set for this problem since values may show up in eithera
orb
multiple times, so that's out. So instead, we can calculate the frequency of occurrences of values instead, which leads to thisO(a + b)
solution: