Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. Array

Valid Sudoku

ID: 36

PreviousNext PermutationNextPermutation II

Last updated 3 years ago

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.

  2. Each column must contain the digits 1-9 without repetition.

  3. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Input: board = 
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true

Idea

Brute force iteration, construct for each row/column/matrix

char[][] for each row/column/matrix, iterate through

Construct checkvalid function to check repetition

Code

public boolean isValidSudoku(char[][] board) {
        boolean valid = true;
        // check row
        for(int i = 0; i<board.length; i++){
            char[][] row = new char[1][board[0].length];
            row[0] = board[i];
        
            if(!checkValid(row)){
                System.out.println("Row wrong");
                return false;
            }
        }
        for(int i = 0; i<board[0].length; i++){
            char[][] col = new char[board.length][1];
            for(int j = 0; j<board.length; j++){
                col[j][0] = board[j][i];
            }
        
            if(!checkValid(col)){
                System.out.println("Column wrong");
                return false;
            }
        }
        for(int i =0; i<board.length; i+=3){
            for(int j = 0; j<board[0].length; j+=3){
                char[][] matrix = new char[3][3];
                for(int m = 0; m<3; m++){
                    for(int n = 0; n<3; n++){
                        matrix[m][n] = board[i+m][j+n];
                    }
                }
                if(!checkValid(matrix)){
                    System.out.println("Matrix wrong");
                    return false;
                }
            }
        }
        return true;
    }
    
    private boolean checkValid(char[][] subBoard){
        List<Character> seen = new ArrayList<>();
        for(int i = 0; i<subBoard.length; i++){
            for(int j=0; j<subBoard[0].length; j++){
                if(subBoard[i][j]!='.'&&seen.contains(Character.valueOf(subBoard[i][j]))){
                    return false;
                }
                else{
                    seen.add(Character.valueOf(subBoard[i][j]));
                }
            }
        }
        return true;
    }