DEV Community

Discussion on: Daily Challenge #213 - Are they the "same"?

Collapse
 
vidit1999 profile image
Vidit Sarkar • Edited

C++ solution

bool comp(vector<int> a, vector<int> b){
    // if both of them have no element then return true
    if(a.size() == 0 || b.size() == 0)
        return true;

    // if they have different size then return false
    if(a.size() != b.size())
        return false;

    int size = a.size(); // or b.size() as both of them have same size
    unordered_map<int, int> squareCount;

    // squares a number of a, increment squareCount[squareNumber_a] by 1 i.e. squareCount[squareNumber_a]++
    // decrement sqareCount[squareNumber_b] by 1 i.e. squareCount[squareNumber_b]--
    for(int i=0;i<size;i++){
        squareCount[a[i]*a[i]]++;
        squareCount[b[i]]--;
    }

    // if they are "same" then all the values of corresponding
    // keys of sqaureCount will be zero
    // if any one of them is not zero then return false
    for(auto it : squareCount){
        if(it.second != 0)
            return false;
    }
    return true;
}