Welcome to my personal website. I use this space to share some of my thoughts and software that I write in my free time. Sometimes I also publish photos. If you want to get in touch please use LinkedIn or Mastodon.
Given a Sudoku field as a two-dimension list, determine if the field is valid. A valid field only has the number 1-9 once in each sub-field, each row, and each column. The field is not completely filled and we don’t need to check for a valid solution.
The solution for this task is written in the conditions for a valid Sudoku field. We create a set for each row, each column, and each sub-field. If a number is duplicated in any of those, the field is not valid. If we don’t find any violation until the end, the field is valid.
The remaining problem is to filter out the empty fields (.) and to find the correct sub-field. For the second part, we can leverage Pythons full integer division and determine the field as follows: field = 3 * (row // 3) + (column // 3). This yields numbers from [0-8].
This solution is faster than 96% of the submitted solutions at leetcode. It does use O(3*n) memory in the worst case, as every element needs to be stored in each set.
About the author
Friedrich Ewald is an experienced Software Engineer with a Master's degree in Computer Science. He started this website in late 2015, mostly as a digital business card. He is interested in Go, Python, Ruby, SQL- and NoSQL-databases, machine learning and AI and is experienced in building scalable, distributed systems and micro-services at multiple larger and smaller companies.