Easy To Understand JAVA Solution:: Black Shapes or Count No Of Islands


#1
public class Solution {
    public int black(String[] A) {
        int n = A.length;
        int m = A[0].length();
        boolean vis[][] = new boolean[n][m];
        int count = 0;
        
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(A[i].charAt(j) == 'X' && !vis[i][j])
                {
                    DFS(A, i, j, vis);
                    count++;
                }
            }
        }
        return count;
    }
    
    void DFS(String[] A, int i, int j, boolean vis[][])
    {
        if(i < 0 || j < 0 || i >= A.length || j >= A[0].length() || vis[i][j] || 
            A[i].charAt(j) != 'X')
            return;
            
        vis[i][j] = true;
        
        DFS(A, i + 1, j, vis);
        DFS(A, i - 1, j, vis);    
        DFS(A, i, j + 1, vis);
        DFS(A, i, j - 1, vis);
    }
}

#2

really helpful… thanks for sharing.