Skip to content

Instantly share code, notes, and snippets.

@bvsatyaram
Created October 2, 2015 07:32
Show Gist options
  • Save bvsatyaram/dc8fbe3f2fd827a146ed to your computer and use it in GitHub Desktop.
Save bvsatyaram/dc8fbe3f2fd827a146ed to your computer and use it in GitHub Desktop.
Hacker Rank Problem: Grid Search
def check_sub_pattern?(big_grid, small_grid)
big_r = big_grid.length
small_r = small_grid.length
big_c = big_grid[0].length
small_c = small_grid[0].length
(0..(big_r-small_r)).each do |r|
(0..(big_c-small_c)).each do |c|
pattern_found = true
(0...small_r).each do |i|
break if !pattern_found
sub_str = big_grid[r+i][c, small_c]
pattern_found &&= (sub_str == small_grid[i])
end
return true if pattern_found
end
end
return false
end
t = gets.chomp.to_i
t.times do
big_r, big_c = gets.chomp.split(" ").collect!(&:to_i)
big_grid = []
big_r.times do
big_grid.push(gets.chomp)
end
small_r, small_c = gets.chomp.split(" ").collect!(&:to_i)
small_grid = []
small_r.times do
small_grid.push(gets.chomp)
end
puts check_sub_pattern?(big_grid, small_grid) ? "YES" : "NO"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment