Difference between "run code" and "submit" test case outcomes


  • 0
    B

    Every time I submit my code it gets marked as incorrect due to the test case "11" "10" which should yield the answer 1A0B. My code gets this result both when tested locally and with the "run code" option in LeetCode. I've even consulted the help link here, but I believe my code does not have any of the issues listed. I even tried randomly generating 50 different inputs before feeding the specific test case to my code, but it still returned the correct result locally.

    Every test case I've thrown at my code has passed and it's making me suspicious of the judge's quality. Any thoughts?

    int bulls=0;
    int cows=0;

        //collect the digits in secret
        HashMap<Character, ArrayList<Integer>> solutionMap = new HashMap<Character, ArrayList<Integer>>();
    
        for(int i=0; i < guess.length(); i++) {
        	if(solutionMap.containsKey(guess.charAt(i))) {
        		solutionMap.get(guess.charAt(i)).add(i);
        	}
        	else {
        		solutionMap.put(guess.charAt(i), new ArrayList<Integer>());
        		solutionMap.get(guess.charAt(i)).add(i);
        	}
        }
    
        for(int i=0; i < secret.length(); i++) {
        	if(solutionMap.containsKey(secret.charAt(i))) {
        		if(solutionMap.get(secret.charAt(i)).contains(i)) {
        			bulls++;
        			solutionMap.get(secret.charAt(i)).remove(new Integer(i));
        			if(solutionMap.get(secret.charAt(i)).size() == 0) {
        				solutionMap.remove(secret.charAt(i));
        			}
        		}
        		else {
        			cows++;
        		}
        	}
        }
    
        return (String.valueOf(bulls) + "A" + String.valueOf(cows) + "B");

  • 1

    Please check your input carefully. The input is:

    "11"
    "01"
    

    I have just tested your code with the above input. Your code outputs "1A1B" while the expected output is "1A0B".


  • 0

    Hi bdgrider, I went through your code and I guess you haven't fully understand the problem, or the problem's description is not clear for you, for "11"(secret) and "10"(guess), your code doesn't output 1A0B.

    In the first for loop you build a solution map from guess string, and the map looks like this:

    1: [0]
    0: [1]
    

    now in the second loop you go through the secret string, the first character is 1, and the index 0 exists in the array list of key 1 in the solution map, so you mark bulls as 1 and remove key 1 completely, the second character of the secret string is also 1, since key 1 has been removed already, you simply do cows++, thus your solution outputs 1A1B.


Log in to reply
 

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