mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
Add brute force solution
This commit is contained in:
parent
211bb2c23f
commit
79ca68d958
1 changed files with 156 additions and 22 deletions
|
@ -9,32 +9,166 @@
|
|||
# @return {Boolean}
|
||||
|
||||
#
|
||||
# Approach 1:
|
||||
#
|
||||
# Time Complexity:
|
||||
# Approach 1: Hash & Brute Force
|
||||
#
|
||||
def is_valid_sudoku(board)
|
||||
rows = []
|
||||
columns = []
|
||||
grids = []
|
||||
|
||||
# make each row into a hash to track for duplicated values
|
||||
board.each do |row|
|
||||
row_hash = Hash.new(0)
|
||||
row.each do |num|
|
||||
next if num == "."
|
||||
row_hash[num] += 1
|
||||
end
|
||||
rows << row_hash
|
||||
end
|
||||
|
||||
# check each row hash for duplicated value
|
||||
rows.each do |row|
|
||||
row.each do |k, v|
|
||||
if v > 1
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# make each column into a hash to track for duplicated values
|
||||
(0..8).each do |i|
|
||||
column_hash = Hash.new(0)
|
||||
board.each do |row|
|
||||
next if row[i] == "."
|
||||
column_hash[row[i]] += 1
|
||||
columns << column_hash
|
||||
end
|
||||
end
|
||||
|
||||
# check each column hash for duplicated value
|
||||
columns.each do |column|
|
||||
column.each do |k, v|
|
||||
if v > 1
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# make each 3x3 grid into a hash to track for duplicated values
|
||||
(0..2).each do |i|
|
||||
grid_hash = Hash.new(0)
|
||||
board.first(3).each do |row|
|
||||
next if row[i] == "."
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board[3..5].each do |row|
|
||||
next if row[i] == "."
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board.each do |row|
|
||||
next if row[i] == "."
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
end
|
||||
(3..5).each do |i|
|
||||
grid_hash = Hash.new(0)
|
||||
board.first(3).each do |row|
|
||||
next if row[i] == "."
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board[3..5].each do |row|
|
||||
next if row[i] == "."
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board.each do |row|
|
||||
next if row[i] == "."
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
end
|
||||
(6..8).last(3).each do |i|
|
||||
grid_hash = Hash.new(0)
|
||||
board.first(3).each do |row|
|
||||
next if row[i] == "."
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board[3..5].each do |row|
|
||||
next if row[i] == "."
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board.each do |row|
|
||||
next if row[i] == "."
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
end
|
||||
|
||||
# check each grid hash for duplicated value
|
||||
grids.each do |grid|
|
||||
grid.each do |k, v|
|
||||
if v > 1
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
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"]]
|
||||
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"]]
|
||||
print(is_valid_sudoku(board))
|
||||
# => true
|
||||
board = [["8","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"]]
|
||||
|
||||
board = [["8","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"]]
|
||||
print(is_valid_sudoku(board))
|
||||
# => false
|
||||
# => false
|
||||
# explanation: duplicated value in column
|
||||
|
||||
board = [["8","3",".",".","7",".","3",".","."],
|
||||
["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"]]
|
||||
print(is_valid_sudoku(board))
|
||||
# => false
|
||||
# explanation: duplicated value in row
|
||||
|
||||
board = [["8","3",".",".","7",".",".",".","."],
|
||||
["6",".",".","1","9","5",".",".","."],
|
||||
[".","9","8",".",".",".",".","6","."],
|
||||
[".",".",".",".","6",".",".",".","3"],
|
||||
["4",".",".","8",".","3",".",".","1"],
|
||||
["7",".",".",".","2",".",".",".","6"],
|
||||
[".","6",".",".",".",".","2","8","."],
|
||||
[".",".",".","4","1","9",".",".","5"],
|
||||
[".",".",".",".","8",".",".","7","9"]]
|
||||
print(is_valid_sudoku(board))
|
||||
# => false
|
||||
# explanation: duplicated value in 3x3 grid
|
Loading…
Reference in a new issue