LeetCode之Find the Difference

简介: LeetCode之Find the Difference

1、题目

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.

please:

Input:

s = "a"

t = "aa"

Output:

a


2、代码实现

public class Solution {
    public static char findTheDifference(String s, String t) {
      if (s == null || t.length() == 0) 
        return t.charAt(0);
      if (t == null || t.length() == 0) 
        return s.charAt(0);
      if (s == null && t == null)
        return 0;
      int[] a = new int[30];
      char[] tChars = t.toCharArray();
      char[] sChars = s.toCharArray();
      int sLength = s.length();
      int tLength = t.length();
      if (sLength > tLength) {
        for (int i = 0; i < sChars.length; i++) {
           if (a[sChars[i] - 97] != 0)
             a[sChars[i] - 97] = ++(a[sChars[i] - 97]);
           else
             a[sChars[i] - 97] = 2;
        }
        for (int i = 0; i < tChars.length; i++) {
          a[tChars[i] - 97] = --(a[tChars[i] - 97]); 
          }
      } else {
        for (int i = 0; i < tChars.length; i++) {
          if (a[tChars[i] - 97] != 0)
           a[tChars[i] - 97] = ++(a[tChars[i] - 97]);
         else
           a[tChars[i] - 97] = 2;
          }
        for (int i = 0; i < sChars.length; i++) {
          a[sChars[i] - 97] = --(a[sChars[i] - 97]); 
          }
      }
      for (int i = 0; i < 30; i ++) {
        if (a[i] >= 2) {
          return (char) (i + 97);
        } 
      }
      return 0;
    }
}

3、总结

看到2个字符串对比,我么可以先转化为字符数组,下表从A - 65 活着  a - 95  开始,也就是从下表0开始,然后要注意2个字符串里面可能包含同样的元素有几个的情况,相同就往上加,另外一个就减,但是他们最多相差1个字符,所以,我们可可以肯定,比我们一开始设置的大,也就是3,然后如果没有重复的数据,那么一样的就为1,肯定有一个为2.



相关文章
|
8月前
|
Java
Leetcode 295. Find Median from Data Stream
在一个有序数组中找中位数,但需要支持再数组中添加新的元素。本来是有序里的,可以很轻易就查到中位数,但如果添加新数字后,不一定有序。如果先对数组排序,那代价就比较大了,每次排序时间复杂度O(n*log(n)),看discuss发现了一种很巧妙的解法,可以把添加数据的时间复杂度降低到O(log(n)) ,查询中位数O(1)。
30 0
|
8月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
28 0
|
算法 Python
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
LeetCode 1160. 拼写单词 Find Words That Can Be Formed by Characters
|
JavaScript 索引
LeetCode 436. Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
65 0
LeetCode 436. Find Right Interval
LeetCode 389. Find the Difference
给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。
89 0
LeetCode 389. Find the Difference
|
索引
LeetCode 373. Find K Pairs with Smallest Sums
给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。 找到和最小的 k 对数字 (u1,v1), (u2,v2) ... (uk,vk)。
124 0
LeetCode 373. Find K Pairs with Smallest Sums
|
算法 Python
LeetCode 295. Find Median from Data Stream
中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
74 0
LeetCode 295. Find Median from Data Stream
|
索引
LeetCode 287. Find the Duplicate Number
给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
72 0
LeetCode 287. Find the Duplicate Number
|
索引
LeetCode 162. Find Peak Element
给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
80 0
LeetCode 162. Find Peak Element
|
算法
LeetCode Find Minimum in Rotated Sorted Array II
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 注意数组中可能存在重复的元素。
69 0
LeetCode Find Minimum in Rotated Sorted Array II