Skip to content

Commit

Permalink
Day 59 by admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh971 authored Apr 28, 2023
1 parent a402643 commit 18c24f1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Day 59/Geek's Village and Wells_GFG/Geek's_Village_and_Wells.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Solution{
public:
vector<vector<int>> chefAndWells(int n,int m,vector<vector<char>> &c){
// Code here
vector<vector<int>> vis(n,vector<int>(m,0));
vector<vector<int>> ans(n,vector<int>(m,-1));
queue<pair<int,pair<int,int>>> q;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(c[i][j]=='N') {
ans[i][j]=0;
vis[i][j]=1;
}
if(c[i][j]=='W'){
vis[i][j]=1;
q.push({0,{i,j}});
ans[i][j]=0;
}
if(c[i][j]=='.') ans[i][j]=0;
}
}
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
while(!q.empty())
{
int row = q.front().second.first;
int col = q.front().second.second;
int dis = q.front().first;
q.pop();
for(int i=0;i<4;i++)
{
int newi = row + dx[i];
int newj = col + dy[i];
if(newi>=0&&newi<n&&newj>=0&&newj<m&&vis[newi][newj]==0&&(c[newi][newj]=='H'||c[newi][newj]=='.'))
{
if(c[newi][newj]=='H') ans[newi][newj] = 2*(dis+1);
q.push({dis+1,{newi,newj}});
vis[newi][newj]=1;
}
}
}
return ans;
}
};
32 changes: 32 additions & 0 deletions Day 59/Similar String Groups_Leetcode/Similar_String_Groups.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int numSimilarGroups(vector<string>& A) {
int ans = 0;
vector<bool> seen(A.size());

for (int i = 0; i < A.size(); ++i)
if (!seen[i]) {
dfs(A, i, seen);
++ans;
}

return ans;
}

private:
// Dfs on string A[i]
void dfs(const vector<string>& A, int i, vector<bool>& seen) {
seen[i] = true;
for (int j = 0; j < A.size(); ++j)
if (!seen[j] && isSimilar(A[i], A[j]))
dfs(A, j, seen);
}

bool isSimilar(const string& X, const string& Y) {
int diff = 0;
for (int i = 0; i < X.length(); ++i)
if (X[i] != Y[i] && ++diff > 2)
return false;
return true;
}
};

0 comments on commit 18c24f1

Please sign in to comment.