269. Alien Dictionary

becuse this Q is about order, use topology sort

be careful this case

exception case is : 
for ex: 
ab
a.  => return ""

if (s1.length() > s2.length() && s2.equals(s1.substring(0, s2.length()))) {
    return "";
}

and duplicate path should not add to map, and indgree

Set<Character> set = map.getOrDefault(s[j], new HashSet<>());
if (!set.contains(t[j])) {
    set.add(t[j]);
    map.put(s[j], set);
    indegree.put(t[j], indegree.getOrDefault(t[j], 0)+1);
}

沒有判斷 s -> t 是否存在重複的話, 這個 case 會不過

最後還要檢查 res 長度是不是跟 indegree 長度一樣, 不然這 test case fail

Last updated

Was this helpful?