C++ solution using a single hashset


#1

int Solution::isValidSudoku(const vector &A) {
unordered_set uset;
for(int i =0;i<A.size();i++)
{
for(int j=0;j<A.size();j++)
{
char c = A[i][j];
if(c!=’.’)
{
string s1 = "At row " + to_string(i) + c;
string s2 = "At column " + to_string(j) + c;
string s3 = "At sub box " + to_string(i/3) + " - " + to_string(j/3) + c;
if(uset.find(s1)!=uset.end() || uset.find(s2)!=uset.end() || uset.find(s3)!=uset.end())
{
return false;
}
else
{
uset.insert(s1);
uset.insert(s2);
uset.insert(s3);
}
}
}
}
return true;
}