Title: Find the difference


Given two strings s and t, they contain only lowercase letters. The string t is randomly rearranged by the string S, and a letter is added at the random position. Please find the letter added to t.Copy the code

Example:


Input: S = "abcd" t = "abcde" Output: e Explanation: 'e' is the letter that is added.Copy the code

Think about:


This problem converts the s and T strings into an array of characters, then converts each char into an int, summing it, and then subtracting it back to char as the added character.Copy the code

Implementation:


class Solution { public char findTheDifference(String s, String t) { char[] schars = s.toCharArray(); char[] tchars = t.toCharArray(); int sSum = 0; int tSum = 0; for (int count = 0; count < s.length(); count++) { sSum += Integer.valueOf(schars[count]); } for (int count = 0; count < t.length(); count++) { tSum += Integer.valueOf(tchars[count]); } return (char) (tSum - sSum); }}Copy the code