Find the Difference

原创 2016年08月29日 12:24:14

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.
思路:注意一点就是:最后return的时候,计算char的index位移,需要转换成char。

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...

【Leetcode】389. Find the Difference

方法一: 思路: 利用一个HashMap,先统计s字符串中每个字符出现的次数,然后遍历t字符串,若该字符未包含在HashMap中或出现次数等于0,立即返回该字符,否则,该字符出现的次数减一。 pub...

HDU--3885[Find the Difference] 暴搜

题意: 就是QQ里的大家来找茬= =!。给两个n*m的整数矩形,求两个矩形间不同的小矩形,输出个数以及每个矩形的左上角、右下角的坐标。要求每个独立的小矩形满足至少有一个点不相同,他的周围一圈是相同的...

Tutorial: 389. Find the Difference

LeetCode算法题:389. Find the Difference的解法。
  • atmiao
  • 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 [...

LeetCode 389. Find the Difference

题意两个字符串s,t,都是小写字母,t由s中的字符和新增的一个字母随机排列组合而成,找出新增的字符!思路最直接的思路是HashMap,根据字符串s建立一个HashMap,key是字母,value是字母...

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个字)