Skip to content

Done Backtracking-3#1088

Open
pranjay01 wants to merge 1 commit intosuper30admin:masterfrom
pranjay01:master
Open

Done Backtracking-3#1088
pranjay01 wants to merge 1 commit intosuper30admin:masterfrom
pranjay01:master

Conversation

@pranjay01
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

N-Queens (Problem1.py)

Strengths:

  • The solution correctly implements backtracking with efficient conflict checking using sets for columns, diagonals, and anti-diagonals.
  • The code is well-structured with helper functions for clarity.
  • The use of sets for tracking conflicts reduces the time complexity of checking validity to O(1) per position, which is optimal.

Areas for improvement:

  1. The function name noCoflict has a typo (should be noConflict). It's important to use correct spelling for readability.
  2. The columnPositionPerRow array is reset to 0 in the backtracking step (columnPositionPerRow[row] = 0), but this is unnecessary because each row's value is overwritten in the next iteration. It can be removed to avoid confusion.
  3. The convertAndAddToResult function could be made more efficient by using a list of strings directly instead of building a temporary list, but the current approach is acceptable.
  4. Adding comments to explain the diagonal and anti-diagonal set keys (row-j and row+j) would improve readability for others who might review the code.

Overall, the solution is excellent and follows best practices for backtracking problems.

VERDICT: PASS


Word Search (Problem2.py)

Your solution has a good structure and you are on the right track with DFS and backtracking. However, there are several critical issues:

  1. Missing Return Statement: In the helper function, if none of the directions lead to a solution, you must return False. Currently, the function does not return anything in that case, which will cause the recursion to break. Add return False at the end of the function.

  2. Current Cell Validation: Although your approach of checking the neighbors is correct, note that the helper function is called only when the previous character was matched. So the function does not need to validate the current cell. However, the function should be designed to validate the current cell if it were called for the current index. Actually, in your implementation, the outer loop validates the first character, and then helper(1, i, j) is called. Inside helper, it tries to match the next character (index1) with neighbors. This is correct. But the function should be generalized to start from the current index. Consider rewriting the helper function to take (row, column) and wordIndex, and first check if (row, column) is valid and matches word[wordIndex]. This would make the function more symmetric and avoid the need for separate handling of the first character. The reference solution does this: it checks the current cell first.

  3. Variable Names: Avoid using dir as a variable name because it is a built-in function in Python. Use something like directions instead.

  4. Efficiency: Using a set to track visited indexes is acceptable, but it might be slower than the common approach of temporarily marking the board (e.g., with a special character like '#') and then backtracking. The set operations (add, remove) have overhead, and membership testing is O(1) but still has cost. The board modification method is more efficient in practice because it avoids the set operations.

  5. Base Case: Your base case (checking if wordIndex == wordLength) is correct. But make sure to return True only when the entire word is matched.

  6. Index Checks: Your index checks for neighbors are correct: r+row>=0 and r+row<m and c+column>=0 and c+column<n.

Recommendation: Restructure the helper function to first check the current cell. This will simplify the code and make it more robust. Here is a revised version of your code:

class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        directions = [(0,-1), (-1,0), (0,1), (1,0)]
        m = len(board)
        n = len(board[0])

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants