Hash Map
class Solution {
public:
bool uniqueOccurrences(vector<int>& arr) {
unordered_map<int, int> mp;
unordered_map<int, int> mp2;
for(int i = 0; i < arr.size(); i++) {
if(mp.find(arr[i]) != mp.end())
mp[arr[i]]++;
else
mp[arr[i]] = 1;
}
for(auto it = mp.begin(); it != mp.end(); it++) {
if(mp2.find(it->second) != mp2.end())
mp2[it->second]++;
else
mp2[it->second] = 1;
}
for(auto it = mp2.begin(); it != mp2.end(); it++) {
if(mp2[it->first] >= 2)
return false;
}
return true;
}
};
Q2: Equal Row and Column Pairs 79/80 :)
class Solution {
public:
int equalPairs(vector<vector<int>>& grid) {
unordered_map<int, int> rowMap;
unordered_map<int, int> colMap;
for(int i = 0; i < grid[0].size(); i++) {
int sum = 0;
for(int j = 0; j < grid[0].size(); j++) sum += grid[i][j];
rowMap[i] = sum;
}
for(int i = 0; i < grid[0].size(); i++) {
int sum = 0;
for(int j = 0; j < grid[0].size(); j++) sum += grid[j][i];
colMap[i] = sum;
}
int countSum = 0;
for(int i = 0; i < grid[0].size(); i++) {
for(int j = 0; j < grid[0].size(); j++) {
if(rowMap[i] == colMap[j]) {
bool isEqual = true;
for(int k = j; k < grid[0].size(); k++) {
if(grid[i][k] != grid[k][j]) isEqual = false;
}
if(isEqual) countSum++;
}
}
}
return countSum;
}
};
Last updated
Was this helpful?