Why is my ans getting wrong ans


#1

#define pb push_back
vector adj[5005];
int Solution::solve(string a, string b, vector &c) {
c.pb(a);
c.pb(b);
int i,j,k;
int n=c.size();
int m=c[0].size();
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
int diff=0;
for(k=0;k<m;k++) if(c[i][k]!=c[j][k]) diff++;
if(diff==1){
adj[i].pb(j);
adj[j].pb(i);
}
}
}
//bfs
queue q;
int dis[n];
int vis[n];
for(i=0;i<n;i++){
dis[i]=100000;
vis[i]=0;
}

q.push(n-2);
dis[n-2]=0;
vis[n-2]=1;

while(!q.empty()){
    int s=q.front();
    q.pop();
    for(auto u:adj[s]){
        if(vis[u]) continue;
        vis[u]=1;
        dis[u]=dis[s]+1;
        q.push(u);
    }
}
if(dis[n-1]==100000) return 0;
return dis[n-1]+1;

}