[LeetCode]--389. 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 char findTheDifference(String s, String t) {
        int[] count = new int[248];
        for (int i = 0; i < s.length(); i++)
            count[s.charAt(i) - 'a']++;
        for (int i = 0; i < t.length(); i++) {
            count[t.charAt(i) - 'a']--;
            if (count[t.charAt(i) - 'a'] < 0)
                return t.charAt(i);
        }
        return ' ';
    }

用两个map记录字符及字符出现的次数。

public char findTheDifference(String s, String t) {
        Map<Character, Integer> sMap = new HashMap<Character, Integer>();
        Map<Character, Integer> tMap = new HashMap<Character, Integer>();
        for (int i = 0; i < t.length(); i++) {
            if (i < s.length())
                if (!sMap.containsKey(s.charAt(i)))
                    sMap.put(s.charAt(i), 1);
                else
                    sMap.put(s.charAt(i), sMap.get(s.charAt(i)) + 1);

            if (!tMap.containsKey(t.charAt(i)))
                tMap.put(t.charAt(i), 1);
            else
                tMap.put(t.charAt(i), tMap.get(t.charAt(i)) + 1);
        }
        for (Character c : tMap.keySet()) {
            if (!sMap.containsKey(c))
                return c;
            if (sMap.containsKey(c) && tMap.get(c) > sMap.get(c))
                return c;
        }
        return ' ';
    }
时间: 2025-01-20 16:58:02

[LeetCode]--389. Find the Difference的相关文章

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode]135.Candy

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

[LeetCode]164.Maximum Gap

题目 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-ne

[LeetCode] Split Linked List in Parts 拆分链表成部分

Given a (singly) linked list with head node root, write a function to split the linked list into k consecutive linked list "parts". The length of each part should be as equal as possible: no two parts should have a size differing by more than 1.

代码分析-leetcode:Scramble String问题

问题描述 leetcode:Scramble String问题 题目:Given a string s1 we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = ""great"": great / gr eat / / g r e at /

c++-leetcode Median of Two Sorted Arrays

问题描述 leetcode Median of Two Sorted Arrays class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { if(nums1.size()==0){ if(nums2.size()%2==0)return ((double)nums2[nums2.size()/2]+nums2[nums2.size()/2-1])/2.0; else ret

[LeetCode]61.Rotate List

[题目] Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. [题意] 给定一个链表,向右旋转k个位置,其中k是非负的. [分析] 先遍历一遍,得出链表长度 len,注意 k 可能大于

[LeetCode]91.Decode Ways

题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 - 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encode

api-AE + c# 开发 pTopologicalOper.Difference算法 API报错

问题描述 AE + c# 开发 pTopologicalOper.Difference算法 API报错 //原始线图层 IFeatureClass pFeatureClass = pLayer.FeatureClass; //获取要素 IFeature pFirstFeature = pFeatureClass.GetFeature(85); IFeature pSecondFeature = pFeatureClass.GetFeature(420); //获取Geomtry并统一投影坐标系