# Find the Difference

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
'e' is the letter that was added.

public class Solution {
public char findTheDifference(String s, String t) {
int[] chars = new int[26];
for(int i=0; i<t.length(); i++){
char c = t.charAt(i);
chars[c-'a']++;
}

for(int i=0; i<s.length(); i++){
char c = s.charAt(i);
chars[c-'a']--;
}

for(int i=0; i<chars.length; i++){
if(chars[i] != 0 ){
return (char)('a'+i);
}
}
return t.charAt(0);
}
}

• 本文已收录于以下专栏：

## [LeetCode]389. Find the Difference(找不同)

389. Find the DifferenceGiven two strings s and t which consist of only lowercase letters. String t...

## LeetCode之Find the Difference

1、题目 Given two strings s and t which consist of only lowercase letters. String t is generate...

## [LeetCode]242.Valid Anagram&383. Ransom Note&389. Find the Difference

242.Valid Anagram 383. Ransom Note 389. Find the Difference

## 389. Find the Difference

Given two strings s and t which consist of only lowercase letters.String t is generated by random sh...

## Tutorial: 389. Find the Difference

LeetCode算法题：389. Find the Difference的解法。
• atmiao
• 2016年12月26日 22:18
• 148

## Problem G: Find The Difference

Problem G: Find The Difference Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 214  Solved: 161 [...

## leetcode389. Find the Difference

389. Find the DifferenceGiven two strings s and t which consist of only lowercase letters.String t i...

举报原因： 您举报文章：Find the Difference 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)