Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number of Distinct Island GFG #237

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Dsa/NumberofDistinctIsland.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Number of Distinct Islands
// GFG
class Solution {
public:
void helper(vector<vector<int>> &grid,int i,int j,string &st){
if(i<0 || j<0 || i>=grid.size() || j>=grid[0].size() || grid[i][j]==0)return ;
grid[i][j]=0;
st=st+"d";
helper(grid,i+1,j,st);
st=st+"u";
helper(grid,i-1,j,st);
st=st+"r";
helper(grid,i,j+1,st);
st=st+"l";
helper(grid,i,j-1,st);
}
int countDistinctIslands(vector<vector<int>>& grid) {
// code here
int count=0;
int n=grid.size();
int m=grid[0].size();
unordered_map<string,int> mp;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j]==1){
string st="";
helper(grid,i,j,st);
if(mp.find(st)==mp.end()){
count++;
}
mp[st]++;
}
}
}
return count;
}
};