Can we fix the Memory Limit Exceeded error?

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.

Errors:

Line 63: error: cannot find symbol list.add(stringToStringList(cols.toString())); ^ symbol: method stringToStringList(String) location: class MainClass

Line 83: error: cannot find symbol List> tickets = stringToString2dList(line); ^ symbol: method stringToString2dList(String) location: class MainClass

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);
        }
    }
}

Problem: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order starting 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.

Errors:

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

Solution:

The errors indicate that the methods stringToStringList and stringToString2dList are not defined in the MainClass. Therefore, we need to define these methods in the MainClass or import them from another class.

Here’s the updated code with the missing methods defined in the MainClass:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.eclipsesource.json.JsonArray;

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<String> stringToStringList(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        List<String> res = new ArrayList<>();
        if (input.length() == 0) {
            return res;
        }

        String[] parts = input.split(",");
        for (String part : parts) {
            res.add(part.trim());
        }

        return res;
    }

    public static List<List<String>> stringToString2dList(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);
        }
    }
}