Test 32 is giving wrong answer on Onine Tester but gives correct on my local


  • 0
    G

    Does anyone know why online tester is making test case 32 fail even though it works fine on my loca;

    Here is the code

    public class Solution {
        public String getHint(String secret, String guess) {
       
        	char[] secretChar = secret.toCharArray();
        	char[] guessChar = guess.toCharArray();
        	Set <String>secretSet = new HashSet<String>();
    
        	for(int i=0;i<secret.length();i++) {
        		secretSet.add(String.valueOf(secret.charAt(i)));
        	}
        	
        	// now since length of both arrays are equal we will first search of characater
        	int bullCounter=0;
        	int cowCounter=0;
        	
        	for(int i=0;i<guessChar.length-1;i++) {
        		if(secretChar[i]==guessChar[i]) {
        			++bullCounter;
        		}
        		else{
        			if(secretSet.contains(String.valueOf(guessChar[i]))) {
        				secretSet.remove(String.valueOf(guessChar[i]));
        				++cowCounter;
        			}
        		}
        	}
           
        	String result = bullCounter+"A"+cowCounter+"B";
        	System.out.println(result);
        	return result;
        }
    }

  • 0
    T

    I think it would help if you shared the failng test case. Test cases are not public afaik, so we can't look up by number.


  • 0
    T

    You should use Set<Character> that would make the code cleaner. (char <-> Character conversion is automatic in Java: auto-boxing)


Log in to reply
 

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