@@ -0,0 +1,1777 @@
+package problems;%0A%0Aimport java.util.*;%0A%0A/**%0A * Leetcode: Reconstruct Itinerary%0A * Created by alan on 2/23/2016.%0A */%0Apublic class ReconstructItinerary %7B%0A    public List%3CString%3E findItinerary(String%5B%5D%5B%5D tickets) %7B%0A        Map%3CString, List%3CString%3E%3E m = new HashMap%3C%3E();%0A        for (String%5B%5D ticket : tickets) %7B%0A            if (m.containsKey(ticket%5B0%5D))%0A                m.get(ticket%5B0%5D).add(ticket%5B1%5D);%0A            else %7B%0A                final List%3CString%3E ls = new ArrayList%3C%3E();%0A                ls.add(ticket%5B1%5D);%0A                m.put(ticket%5B0%5D, ls);%0A            %7D%0A        %7D%0A        for (String s : m.keySet())%0A            Collections.sort(m.get(s));%0A        //System.out.println(Arrays.toString(m.entrySet().toArray()));%0A%0A        return find(m, %22JFK%22, tickets.length);%0A    %7D%0A%0A    public List%3CString%3E find(Map%3CString, List%3CString%3E%3E m, String start, int count) %7B%0A        List%3CString%3E it = new ArrayList%3C%3E();%0A        it.add(start);%0A        if (count == 0 %7C%7C !m.containsKey(start))%0A            return it;%0A%0A        for (int i = 0; i %3C m.get(start).size(); i++) %7B%0A            String s = m.get(start).remove(i);%0A            List%3CString%3E next = find(m, s, count - 1);%0A            if (next.size() == count) %7B%0A                it.addAll(next);%0A                return it;%0A            %7D%0A            m.get(start).add(i, s); // put s back in order%0A        %7D%0A        return it;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        ReconstructItinerary ri = new ReconstructItinerary();%0A        //String%5B%5D%5B%5D tickets = %7B%7B%22JFK%22,%22SFO%22%7D,%7B%22JFK%22,%22ATL%22%7D,%7B%22SFO%22,%22ATL%22%7D,%7B%22ATL%22,%22JFK%22%7D,%7B%22ATL%22,%22SFO%22%7D%7D;%0A        String%5B%5D%5B%5D tickets = %7B%7B%22JFK%22,%22KUL%22%7D,%7B%22JFK%22,%22NRT%22%7D,%7B%22NRT%22,%22JFK%22%7D%7D;%0A%0A        System.out.println(Arrays.deepToString(tickets));%0A        System.out.println(ri.findItinerary(tickets));%0A    %7D%0A%7D%0A

Add ReconstructItinerary