Simple Java Solution (9ms)


  • 0
    S

    Using ASCII to count how many times each letter appears.

    public class Solution {
        public char findTheDifference(String s, String t) {
                    int[] index1 = new int[26];
    		int[] index2 = new int[26];
    		
    		for(int i=0;i<s.length();i++){
    		     index1[s.charAt(i)-97]++;
    		}
    		for(int j=0;j<t.length();j++){
    	             index2[t.charAt(j)-97]++;
    		}
    		for(int k=0;k<index1.length;k++){
    		     if(index1[k]!=index2[k]){
    			 return (char)(k+97);
    		     }
    		}
            
            return '0';
        }
    }
    

Log in to reply
 

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