Nice Question, should have been more interesting if added more test cases


#1

int box(int i,int j,int rows,int cols)
{
if(i<3)
{
if(j<3) return 1;
else if(j<6) return 2;
else return 3;
}
else if(i<6)
{
if(j<3) return 4;
else if(j<6) return 5;
else return 6;

}
else
{
    if(j<3) return 7;
    else if(j<6) return 8;
    else return 9;
}

}

int Solution::isValidSudoku(const vector &A) {
unordered_map<int,vector<pair<int,int>>> mp;
unordered_map<int,vector> mp2;
int rows = A.size();
int cols = A[0].size();
for(int i = 0;i<A.size();i++)
{
for(int j = 0;j<A[i].size();j++)
{
if(A[i][j] != ‘.’){

            int val = A[i][j] - '0';
            if(val>9 || val<1)return 0;
            pair<int,int>  t =  make_pair(i,j);
            if(mp.find(val) == mp.end())
            {
                vector<pair<int,int>> tt;
                tt.push_back(t);
                mp[val] = tt; 
            }
            
            else
            {
                for(auto x:mp[val])
                {
                    int r = x.first;
                    int c = x.second;
                    if(i == r || j == c)
                        return 0;
                }
                mp[val].push_back(make_pair(i,j));
            }
            if(mp2.find(val) == mp2.end())
            {
                vector<int> x;
                x.push_back(box(i,j,rows,cols));
                mp2[val] =x; 
            }
            else
            {
                int t = box(i,j,rows,cols);
                for(auto x:mp2[val])
                {
                    if (x == t)return 0;
                }
                mp2[val].push_back(t);
            }
     }
    }
}


return 1;

}