Java Solution, think of the problem as 'Find the missing value'


  • 1
    V
    public class Solution {
        public char findTheDifference(String s, String t) {
            if(s == null || t == null) return ' ';
            if(t.length() != (s.length() + 1)) return ' ';
            
            int charCountS = 0, charCountT = 0;
            for(char c : s.toCharArray()) {
                charCountS += c;
            }
            
            for(char c : t.toCharArray()) {
                charCountT += c;
            }
            // After you have found the sum of the numbers
           // Subtract the ascii value for the character out
            return (char)(charCountT - charCountS);
        }
    }
    

Log in to reply
 

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