Solution by <abhileshpatil>


  • 0
    A

    java

    class Solution {
        public boolean isValidSudoku(char[][] board) {
            Boolean ans=true;
            for(int i=0;i<9;i++)
            {
                ArrayList<Character> temp=new ArrayList<Character>();
                for(int j=0;j<9;j++)
                {
                    if(board[i][j]!='.')
                    {
                    if(!temp.contains(board[i][j]))
                    {
                    temp.add(board[i][j]);    
                    }
                        else
                        {
                            ans=false;
                        }
                    }
                }
                ArrayList<Character> temp1=new ArrayList<Character>();
                for(int j=0;j<9;j++)
                {
                    if(board[j][i]!='.')
                    {
                    if(!temp1.contains(board[j][i]))
                    {
                    temp1.add(board[j][i]);    
                    }
                        else
                        {
                            ans=false;
                        }
                    }
                }
            }
            int i=0,j=0,m=0,n=0;
            HashSet<Character>hs=new HashSet<Character>();
            for(int k=0;k<7;k=k+3)
            {
            i=0;m=0;
            while(i<9 && m<3)
            {
                n=0;
                j=k;
                while(j<9 && n<3)
                {
                    if(board[i][j]!='.')
                    {
                    if(!hs.contains(board[i][j]))
                    {
                        hs.add(board[i][j]);
                        j++;
                        n++;
                    }
                    else
                    {
                        return false;
                    }
                    }
                    else
                    {
                        j++;
                        n++;
                    }
                }
                i=i+1;
                m=m+1;
                if(m==3)
                {
                    m=0;
                    hs.clear();
                }
            }
            }
            return ans;      
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.