simple java solution


  • 0
    G
    public class Solution {
        public char findTheDifference(String s, String t) {
            char[] sc = s.toCharArray();
            char[] tc = t.toCharArray();
            
            int[] sCount = new int[256];
            int[] tCount = new int[256];
            
            for(int i = 0; i < sc.length; i++) {
                sCount[sc[i]]++;
            }
            
            for(int i = 0; i < tc.length; i++) {
                sCount[tc[i]]--;
            }        
            
            for(int i = 0; i < tc.length; i++) {
                if (sCount[tc[i]] < 0 || sCount[tc[i]] >= 1) 
                    return tc[i];            
            }        
    
            return tc[0];
        }
    }
    

Log in to reply
 

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