Java code for 389. Find the Difference


  • 0
    W
    public class Solution {
        public char findTheDifference(String s, String t) {
            int[] sHash = new int[26];
    		int[] tHash = new int[26];
    		for(int i=0;i<s.length();i++){
    			sHash[s.charAt(i)-'a']++;
    		}
    		for(int i=0;i<t.length();i++){
    			tHash[t.charAt(i)-'a']++;
    		}
    		char x='a';
    		for(int i=0;i<26;i++){
    			if(sHash[i]!=tHash[i])
    				x= (char)(i+'a');
    		}
    		return x;
        }
    }
    

Log in to reply
 

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