leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

valid-sudoku.dart (2020B)


      1 //Given a sudoku board check to make sure that all of the initial
      2 //numbers on the board do not conflict with each other.
      3 //To do this I checked for each value if the same value was in the same
      4 //row, column, or square making sure to skip over the current value as to not
      5 //compare it with itself. If it was unique I would continue iterating until finding the next
      6 //number and would repeat the process until reaching the end. 
      7 //Time: 274ms Beats: 100%
      8 //Memory: 146.1MB Beats: 53.23%
      9 
     10 class Solution {
     11   bool isValidSudoku(List<List<String>> board) {
     12       return valid(board, [0,0]);
     13   }
     14   bool valid(List<List<String>>board , List<int> current_pos){
     15       if(current_pos[0] == 9 && current_pos[1] == 8){
     16           return true;
     17       }
     18       if(current_pos[0] == 9){
     19           current_pos[0] = 0;
     20           current_pos[1] += 1;
     21       }
     22       if(board[current_pos[0]][current_pos[1]] != '.'){
     23           for(int i = 0 ; i < board[0].length ; ++i){
     24               if(i == current_pos[1]){
     25                   continue;
     26               }
     27               if(board[current_pos[0]][i] == board[current_pos[0]][current_pos[1]]){
     28                   return false;
     29               } 
     30           }
     31           for(int i = 0 ; i < board.length ; ++i){
     32               if(i == current_pos[0]){
     33                   continue;
     34               }
     35               if(board[i][current_pos[1]] == board[current_pos[0]][current_pos[1]]){
     36                   return false;
     37               } 
     38           }
     39           for(int i = ((current_pos[0] / 3).toInt() * 3); i < (((current_pos[0] / 3).toInt() * 3)+ 3); ++i){
     40             for(int x = ((current_pos[1] / 3).toInt() * 3); x < ((current_pos[1] / 3).toInt() * 3) + 3; ++x){
     41                 if(i == current_pos[0] && current_pos[1] == x){
     42                     continue;
     43                 }
     44                 if(board[i][x] == board[current_pos[0]][current_pos[1]]){
     45                     return false;
     46                 }
     47             }
     48           }
     49       }
     50       return valid(board, [current_pos[0] + 1, current_pos[1]]);
     51   }
     52 }