educative.io

Educative

Incorrect Output in leetCide

Solution is not working on leetcode platform.

I had to update build the graph step as follow:
// Step - 2 => build the graph
for (int i = 0; i < words.length - 1; i++) {
String word1 = words[i], word2 = words[i+1];
// Check that word2 is not a prefix of word1.
if (word1.length() > word2.length() && word1.startsWith(word2)) {
return “”;
}
for (int j = 0; j < Math.min(word1.length(), word2.length()); j++) {
char parent = word1.charAt(j);
char child = word2.charAt(j);
if(parent != child) { // if the two characters are different
graph.get(parent).add(child); // Add it different character as a child of parent
inDegree.put(child, inDegree.get(child) + 1); // increment indegree
break; // When we encounter first differ character
}

                }
            }
1 Like

Thank you, Neel!! you’re the best !!!