Explain why this answer is wrong?

  • 0


    why is this answer wrong? KUL is lexicographically smaller than NRT. My comparator is ->

        Comparator<String> cmp = new Comparator<String>(){
            public int compare (String o1, String o2) {
                char[] c2 = o2.toCharArray();
                char[] c1 = o1.toCharArray();
                for(int i = 0; i< c1.length;i++) {
                    if(c1[i]<c2[i]) return -1;
                    else if(c2[i]<c1[i]) return 1;
                return o1.length()-o2.length();

    I have also seen other solutions where

    1. an itinerary of 1 ticket is allowed.
    2. shorter itinerary succeeds over longer itinerary (entire reason i used bfs with sorted array in lexicographic order was to achieve this...)

  • 0

    @stephensjlee Same here, my solution correctly returns that 2-city itinerary. The test case is wrong although it is correct to check for loops because they complicate everything.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.