Find the Difference

原创 2016年08月29日 09:20:53
题目:

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.

分析:打表计数

代码:

class Solution {
public:
    char findTheDifference(string s, string t) {
        int slength=s.length();
        int stable[100];
        memset(stable,0,sizeof(stable));
        for(int i=0; i<slength; i++)
        {
            stable[s[i]-'a']++;
        }
        char ans;
        for(int i=0; i<slength+1; i++)
        {
            stable[t[i]-'a']--;
            if(stable[t[i]-'a']<0)
            {
                ans=t[i];
            }
        }
        return ans;
    }
};




相关文章推荐

关于LeetCode中Find the Difference一题的理解

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

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 ...
  • styshoo
  • styshoo
  • 2016年09月29日 23:46
  • 479

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
  • 126
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Find the Difference
举报原因:
原因补充:

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