Java Solution


  • 0
    S
    public char findTheDifference(String s, String t) {
       int[] hash = new int[26];
       char[] sarray = s.toCharArray();
       char[] tarray = t.toCharArray();
       for(int i = 0; i < s.length(); i++){
    	   int num = sarray[i] - 'a';
    	   hash[num]++;
       }
       for(int i = 0; i < t.length(); i++){
    	   int tnum = tarray[i] - 'a';
    	   hash[tnum]--;
    	   if(hash[tnum] < 0){
    		   return (char) ((char)tnum+'a');
    	   }
       }
       return '-';
    }

Log in to reply
 

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