269. alien dictionary
269. alien dictionary https://www.youtube.com/watch?v=wECp0JQVLSM https://leetcode.com/problems/alien-dictionary/discuss/70119/Java-AC-solution-using-BFS/159239 Input: [ "wrt", "wrf", "er", "ett", "rftt" ] Output: "wertf" ========================= key: t w r e val: f e t r tf we rt er wertf ====================== degree key: w r t f e val: 0 1 1 1 1 ================== ["a", "b", "ea", "ec", "ef", "c", "d", "f"] Map b a c d e E bc df f c Indegree e b c d f a 1 1 2 1 2 0 Result = abecdf =========== Input: ["za","zb","ca","cb"] Output: "abzc" Za Zb Ca Cb if(!set.contains(c2)){ set.add(c2); map.put(c1, set); int degree = indegree.get(c2); indegree.put(c2, degree+1); } A-> b would appear twice But we only increase the b’s indegree once So we check if the set contains b already , if yes , we do nothing ===========
class Solution { public String alienOrder(String[] words) { HashMap<Character, Integer> indegree = new HashMap<>(); HashMap<Character, Set<Character>> map = new HashMap<>(); for (String word : words) { for (Character c : word.toCharArray()) { if (!indegree.containsKey(c)) { indegree.put(c, 0); } } } // construct the indegree map and the letter relations map // the indegree map: key is the the char, value is its indegree // the letter relations map : key is the charA, value is a set of chars charA can go to (you can go to after visiting charA) for (int i = 0; i < words.length - 1; i++) { String word1 = words[i]; String word2 = words[i + 1]; int min = Math.min(word1.length(), word2.length()); for (int j = 0; j < min; j++) { char c1 = word1.charAt(j); char c2 = word2.charAt(j); if (c1 != c2) { // we only care about the first different pair of chars from the up and down words if (!map.containsKey(c1)) { map.put(c1, new HashSet<Character>()); // fill in with an empty hashset, so we can add char directly later } Set<Character> set = map.get(c1); if(!set.contains(c2)) { set.add(c2); // the main part of constructing the letter relation map map.put(c1, set); int degree = indegree.get(c2); // the main part of constructing the indegree map indegree.put(c2, degree + 1); // we can't degree++, because it's not in the int[] array. it's in the map. we need to get it and ++, and put it back to the map } break; // we only compare one pair , the first different pair. we don't care about the rest pairs // so here we are breaking out the for loop "for (int j = 0; j < min; j++)" } } } // after we done building indegree map and letter relations map, we can start bfs indegree // first put the char with indegree = 0 into the queue Queue<Character> queue = new LinkedList<>(); for (Character ch : indegree.keySet()) { if (indegree.get(ch) == 0) { queue.offer(ch); } } // this is the main part of bfs indegree // so everytime we poll one char from the queue. we can append the char to stringbuilder // since we know the char we just polled from the queue has indegree 0 StringBuilder sb = new StringBuilder(); while (!queue.isEmpty()) { char current = queue.poll(); sb.append(current); // map might not have the current as a key if (map.containsKey(current)) { // go to its neis , and update the neis indegree, and if its nei's indegree is 0 , add it to the queue for (char nei : map.get(current)) { indegree.put(nei, indegree.get(nei) - 1); if (indegree.get(nei) == 0) { queue.offer(nei); } } } } // like in the course schedule, it's not guareenteed that we can finish all the letters. so need to // check with the indegree map size, because indergee map has all the letters // so it doesn't match, we return "" if (sb.length() != indegree.size()) { return ""; } // else , we can finish all the letters, we can just convert the sb into string return sb.toString(); } }
Using “break” in for loop
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
This will produce the following result −
Output
10
20
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
Example 1:
Input:
[
"wrt",
"wrf",
"er",
"ett",
"rftt"
]
Output: "wertf"
Example 2:
Input:
[
"z",
"x"
]
Output: "zx"
Example 3:
Input: [ "z", "x", "z" ] Output:""Explanation: The order is invalid, so return"".
Note:
- You may assume all letters are in lowercase.
- You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
- If the order is invalid, return an empty string.
- There may be multiple valid order of letters, return any one of them is fine.
posted on 2018-08-09 19:00 猪猪🐷 阅读(183) 评论(0) 收藏 举报
浙公网安备 33010602011771号