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中Find the Difference一题的理解

题目如下: Given two strings s and t which consist of only lowercase letters. String t is generat...

389. Find the Difference

原题链接: https://leetcode.com/problems/find-the-difference/ 题目: Given two strings s and t which cons...

LeetCode笔记:389. Find the Difference

在两个字符串中找出唯一多出的那个字母

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

leetcode算法——389. Find the Difference(基于JAVA)

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

389. Find the Difference [easy] (Python)

题目链接https://leetcode.com/problems/find-the-difference/题目原文 Given two strings s and t which consist...

【LeetCode】389 Find the Difference(java)

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

HDU--3885[Find the Difference] 暴搜

题意: 就是QQ里的大家来找茬= =!。给两个n*m的整数矩形,求两个矩形间不同的小矩形,输出个数以及每个矩形的左上角、右下角的坐标。要求每个独立的小矩形满足至少有一个点不相同,他的周围一圈是相同的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)