Can someone help me correct my program?


  • -1
    P

    I submitted this solution for the problem but it's not accepted. When I run the test case in my IntelliJ Idea I am getting the correct answer but when I submit the solution in Leet, it says its returning wrong answer (null)

    Test Case:
    String[] list1 = {"Shogun","Tapioca Express","Burger King","KFC"};
    String[] list2 = {"Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"};

    Answer returned on my computer: Shogun

    Answer shown on Leet after I submit my solution: []

    Program: '''
    ArrayList<String> favList = new ArrayList<String>();
    String[] favArray = new String[favList.size()];

        int count = 0, countnew = 0;
        Integer res = Integer.MAX_VALUE;
        int curi = 0;
        if (list1.length == 0 || list2.length == 0) {
            favArray = new String[]{};
            return favArray;
        }
    
        else if (list1.length <= list2.length) {
            outerloop:
            for (int i = 0; i < list1.length; i++) {
                for (int j = 0; j < list2.length; j++) {
                    if (list1[i] == list2[j]) {
                        count = i + j;
                        curi = i;
                        favList.add(list1[curi]);
                        break outerloop;
                    }
                }
            }
            for (int i = curi + 1; i < list1.length; i++) {
                for (int j = 0; j < list2.length; j++) {
                    if (list1[i] == list2[j]) {
                        countnew = i + j;
                        if (countnew < count) {
                            favList.removeAll(favList);
                            count = countnew;
                            curi = i;
                            favList.add(list1[curi]);
                        } else if (countnew == count) {
                            favList.add(list1[i]);
                        }
                    }
                }
            }
            favArray = favList.toArray(favArray);
            return favArray;
        }
        else {
            outerloop:
            for (int i = 0; i < list2.length; i++) {
                for (int j = 0; j < list1.length; j++) {
                    if (list2[i] == list1[j]) {
                        count = i + j;
                        curi = i;
                        favList.add(list2[curi]);
                        break outerloop;
                    }
    
                }
            }
            for (int i = curi + 1; i < list2.length; i++) {
                for (int j = 0; j < list1.length; j++) {
                    if (list2[i] == list1[j]) {
                        countnew = i + j;
                        if (countnew < count) {
                            favList.removeAll(favList);
                            count = countnew;
                            curi = i;
                            favList.add(list2[curi]);
                        } else if (countnew == count) {
                            favList.add(list2[i]);
                        }
                    }
                }
            }
            favArray = favList.toArray(favArray);
            return favArray;
        } '''

Log in to reply
 

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