Skip to content

Bug Report for count-number-of-islands #5961

Description

@kjais1720

Bug Report for https://neetcode.io/problems/count-number-of-islands

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

The IDE in the coding playground fails to recognize a valid single quote when comparing a cell's value to '0' in my code, causing several test cases to fail. In contrast, the identical solution generated by neetbot passes all tests without issue.

My solution in Golang:
`
func numIslands(grid [][]byte) int {
if len(grid) == 0 || len(grid[0]) == 0 {
return 0
}
rows := len(grid)
cols := len(grid[0])
visits := make(map[int]map[int]bool, rows)

var dfs func(grid [][]byte, r, c int) bool

dfs = func(grid [][]byte, r, c int) bool {
	rvisits, ok := visits[r]

	if r < 0 || c < 0 || r == rows || c == cols || grid[r][c] == '0' || (ok && rvisits[c] == true) {
		return true
	}

	if ok {
		rvisits[c] = true
	} else {
		visits[r] = map[int]bool{
			c: true,
		}
	}

	if dfs(grid, r+1, c) && dfs(grid, r-1, c) && dfs(grid, r, c+1) && dfs(grid, r, c-1) {
		return true
	}
	return false
}

count := 0
for i, row := range grid {
	for j, val := range row {
		if rvisits, ok := visits[i]; val == 0 || (ok && rvisits[j] == true) {
			continue
		}
		if dfs(grid, i, j) {
			count++
		}
	}
}

return count

}`

The code suggested by neetcode:

`
func numIslands(grid [][]byte) int {
if len(grid) == 0 || len(grid[0]) == 0 {
return 0
}
rows := len(grid)
cols := len(grid[0])
visits := make(map[int]map[int]bool, rows)

var dfs func(grid [][]byte, r, c int) bool

dfs = func(grid [][]byte, r, c int) bool {
	rvisits, ok := visits[r]

	if r < 0 || c < 0 || r == rows || c == cols || grid[r][c] == '0' || (ok && rvisits[c] == true) {
		return true
	}

	if ok {
		rvisits[c] = true
	} else {
		visits[r] = map[int]bool{
			c: true,
		}
	}

	if dfs(grid, r+1, c) && dfs(grid, r-1, c) && dfs(grid, r, c+1) && dfs(grid, r, c-1) {
		return true
	}
	return false
}

count := 0
for i, row := range grid {
	for j, val := range row {
		if rvisits, ok := visits[i]; val == '0' || (ok && rvisits[j] == true) {
			continue
		}
		if dfs(grid, i, j) {
			count++
		}
	}
}

return count

}`

When I run a diffcheck on both, the only difference it points out is this:
`my code: grid[r][c] == 0

neetbot code: grid[r][c] == '0'
`
Funny thing is even when I copy this exact snippet from the neetcode's solution's tab into my my solution's tab, it doesn't work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions