Last active
January 14, 2023 00:23
-
-
Save kezipe/24e7212638fd5362b6459a5372cdf9b3 to your computer and use it in GitHub Desktop.
A function to search words in a 2D character matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func find(word: String, in arr: [[Character]]) -> Bool { | |
let word = Array(word) | |
var index = 0 | |
func check(x: Int, y: Int) -> Bool { | |
// check bounds | |
guard x >= 0, | |
y >= 0, | |
x < arr[0].count, | |
y < arr.count, index < word.endIndex | |
else { return false } | |
// check if matches | |
return word[index] == arr[x][y] | |
} | |
func checkAll(x: Int, y: Int, modifies: [((inout Int, inout Int) -> Void)]) -> Bool { | |
guard check(x: x, y: y) else { return false } | |
for modify in modifies { | |
index = 0 | |
var x = x | |
var y = y | |
// perform transformation and | |
// keep performing transformation if it's good | |
repeat { | |
index += 1 | |
modify(&x, &y) | |
} while check(x: x, y: y) | |
// check if the word has been completed | |
if index == word.endIndex { return true } | |
} | |
return false | |
} | |
// array of transformations, directions to search | |
let operations: [(inout Int, inout Int) -> Void] = [ | |
{ x, y in x -= 1; y -= 1 }, | |
{ x, y in x -= 0; y -= 1 }, | |
{ x, y in x += 1; y -= 1 }, | |
{ x, y in x -= 1; y -= 0 }, | |
// identity transformation ignored: | |
// { x, y in x -= 0; y -= 0 }, | |
{ x, y in x += 1; y -= 0 }, | |
{ x, y in x -= 1; y += 1 }, | |
{ x, y in x -= 0; y += 1 }, | |
{ x, y in x += 1; y += 1 }, | |
] | |
for y in 0 ..< arr.count { | |
for x in 0 ..< arr[0].count { | |
if checkAll(x: x, y: y, modifies: operations) { | |
return true | |
} | |
index = 0 | |
} | |
} | |
return false | |
} | |
let arr: [[Character]] = [ | |
["A", "L", "U", "D"], | |
["C", "O", "B", "E"], | |
["N", "E", "E", "D"], | |
["H", "B", "R", "E"], | |
] | |
print(find(word: "UBER", in: arr) == true) | |
print(find(word: "ALUD", in: arr) == true) | |
print(find(word: "COBE", in: arr) == true) | |
print(find(word: "NEED", in: arr) == true) | |
print(find(word: "ACNH", in: arr) == true) | |
print(find(word: "DEDE", in: arr) == true) | |
print(find(word: "AOEE", in: arr) == true) | |
print(find(word: "DBEH", in: arr) == true) | |
print(find(word: "EEOA", in: arr) == true) | |
print(find(word: "BER", in: arr) == true) | |
print(find(word: "LUD", in: arr) == true) | |
print(find(word: "OBE", in: arr) == true) | |
print(find(word: "EED", in: arr) == true) | |
print(find(word: "CNH", in: arr) == true) | |
print(find(word: "EDE", in: arr) == true) | |
print(find(word: "OEE", in: arr) == true) | |
print(find(word: "BEH", in: arr) == true) | |
print(find(word: "EOA", in: arr) == true) | |
print(find(word: "AONB", in: arr) == false) | |
print(find(word: "BED", in: arr) == false) | |
print(find(word: "LUBER", in: arr) == false) | |
print(find(word: "DEER", in: arr) == false) | |
print(find(word: "BEEB", in: arr) == false) | |
print(find(word: "HEED", in: arr) == false) | |
print(find(word: "EEOL", in: arr) == false) | |
print(find(word: "RDEE", in: arr) == false) | |
print(find(word: "EDEB", in: arr) == false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment