1

Here is a problem: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.

Note:

If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"]. All airports are represented by three capital letters (IATA code). You may assume all tickets form at least one valid itinerary.

Unfortunately i get all the time these errors

Line 63: error: cannot find symbol [in MainClass.java] list.add(stringToStringList(cols.toString())); ^ symbol: method stringToStringList(String) location: class MainClass Line 83: error: cannot find symbol [in MainClass.java] List> tickets = stringToString2dList(line); ^ symbol: method stringToString2dList(String) location: class MainClass 2 errors

Here is my code:

class Solution {
    class Checker implements Comparator<String>{
        @Override
        public int compare(String o1, String o2) {
            return o1.compareToIgnoreCase(o2);
        }
    }
    public List<String> findItinerary(List<List<String>> tickets){
        String begin="JFK";
        final String example=begin;
        List<String> solution = new ArrayList<>();
        long counter=tickets.stream().filter(lister->lister.get(0).equals(example)).count();
        List<String> lexic=tickets.stream().filter(lister->lister.get(0).equals("JFK")).map(p->p.get(1)).distinct().collect(Collectors.toList());
        Comparator<String> comparator =new Checker();
        lexic.sort(comparator);
        solution.add(begin);
        begin=lexic.get(0);
        System.out.println(counter);
        for(int i=0;i<tickets.size();i++) {
            if(tickets.get(i).get(0).equals(begin)) {
                if(!solution.contains(begin)) {
                    solution.add(begin);
                }
                begin=tickets.get(i).get(1);
                solution.add(begin);
                i=-1;
            }
        }
        return solution;
    }

}

public class MainClass {
    public static String[] stringToStringArray(String line) {
        JsonArray jsonArray = JsonArray.readFrom(line);
        String[] arr = new String[jsonArray.size()];
        for (int i = 0; i < arr.length; i++) {
          arr[i] = jsonArray.get(i).asString();
        }
        return arr;
    }

    public static List<List<String>> stringToString2dArray(String input) {
        JsonArray jsonArray = JsonArray.readFrom(input);
        if (jsonArray.size() == 0) {
          return new ArrayList<List<String>>();
        }
        List<List<String>> list = new ArrayList<>(jsonArray.size());
        for (int i = 0; i < jsonArray.size(); i++) {
          JsonArray cols = jsonArray.get(i).asArray();
          list.add(stringToStringList(cols.toString()));
        }
        return list;
    }

    public static String stringListToString(List<String> stringList) {
        StringBuilder sb = new StringBuilder("[");
        for (String item : stringList) {
            sb.append(item);
            sb.append(",");
        }

        sb.setCharAt(sb.length() - 1, ']');
        return sb.toString();
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            List<List<String>> tickets = stringToString2dList(line);

            List<String> ret = new Solution().findItinerary(tickets);

            String out = stringListToString(ret);

            System.out.print(out);
        }
    }
}
jps
  • 20,041
  • 15
  • 75
  • 79
scorpion
  • 9
  • 1
  • 3
  • 1
    You're simply trying to call methods that don't exist. Read the message carefully. look at the methods you're calling. Then look at the methods you've defined. – JB Nizet Jan 01 '20 at 14:57
  • The errors mean that there are no methods named `stringToStringList` and `stringToString2dList` in your class `MainClass`. Looking at your class, there are indeed no such methods, however you do have methods named `stringToStringArray` and `stringToString2dArray`. Solution: Use the correct method names. – Jesper Jan 01 '20 at 14:58
  • Please change your question title to reflect the actual problem you are facing. As it stands, it does nobody any good. – beaker Jan 01 '20 at 18:06

1 Answers1

0

You really don't seem to have a stringToStringList method. You'll have to add one, but luckily, it should be quite easy to reuse the stringToStringArray method you already have:

public static String[] stringToStringList(String line) {
    return Arrays.asList(stringToStringArray(line));
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350