Don't know the reason-Java


  • 0
    F

    I think the implementation is correct, it just can not pass the long test case, wonder if it is related to double for?

    import java.util.ArrayList;
    import java.util.Hashtable;
    import java.util.List;
    public class Solution {
        public char findTheDifference(String s, String t) {
            
            char[]sChar = s.toCharArray();
            char[]tChar = t.toCharArray();
            Hashtable<Character, Character> table = new Hashtable<>();
            for(int i = 0;i < tChar.length;i++){
            	table.put(tChar[i], tChar[i]);
            }
            for(int i = 0;i < sChar.length;i++){
            	if (table.containsKey(sChar[i])){
            		table.remove(sChar[i]);
            	}
            }
            List<Character> answer = new ArrayList<Character>(table.keySet());
    	    return answer.get(0);
        }
    }
    

  • 0
    X

    Wrong answer, think what if s is longer than t.


  • 0
    F

    But it is said "String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was added in t." Doesn't it means t is longer than s?


  • 0
    M

    @fluffy_emory Try the case:

    "a"
    "aa"
    

    You will get it.


  • 0
    F

    @ming328 I've got it! Thank you!


Log in to reply
 

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