*389. Find the Difference (string + map(26)) read problems carefully

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) {
        //in the middle(start) or  in the end
        //get small length
        int sn = s.length();
        int tn = t.length();
        if(sn > tn)
        return helper(s,t);
        else return helper(t,s);
    }
    char helper(String l, String s){ // larger and smaller
        int[] a = new int[26];
        int[] b = new int[26];
        for(int i = 0; i<l.length(); i++){
            a[l.charAt(i) - ‘a‘] ++;
        }
        for(int i = 0; i<s.length(); i++){
            b[s.charAt(i) - ‘a‘] ++;
        }
        for(int i = 0; i<26; i++){
            if(a[i] != b[i]) return (char)(i+‘a‘);
        }
        return ‘a‘;
    }
}

原文地址:https://www.cnblogs.com/stiles/p/leetcode389.html

时间: 2024-08-03 06:58:13

*389. Find the Difference (string + map(26)) read problems carefully的相关文章

总结的一些json格式和对象/String/Map/List等的互转工具类

原文:总结的一些json格式和对象/String/Map/List等的互转工具类 源代码下载地址:http://www.zuidaima.com/share/1550463691508736.htm 总结的一些json格式和对象/String/Map/List等的互转工具类,有需要的可以看看,需要引入jackson-core-asl-1.7.1.jar.jackson-jaxrs-1.7.1.jar.jackson-mapper-asl-1.7.1.jar这三个jar包 package com.

Netscaler数据索引String Map与Pattern Set的区别

Netscaler中建立索引或子集无疑是解耦前后关联事务的非常好的思路,可以大幅度的减少后期策略的修改,简化管理,降低用户的使用门槛.即便是不常操作netscaler但又偶尔有改动策略需求的管理员,你只要告诉他改一个地方的文件即可完成多条策略的更新工作,而不需要他理解复杂的策略逻辑关系.在netscaler中有data set.pattern set.string map等方式来提供不同目的的属性子集以供操作.我们这里先讨论其中两个与字符串相关的类型.以下是手册中对这两个参数的描述:String

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 = "ab

SONObjetc和String Map Bean互转,JSONArray和String List互转

1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.List; 4 import java.util.Map; 5 6 import com.alibaba.fastjson.JSONArray; 7 import com.alibaba.fastjson.JSONObject; 8 9 10 public class JSONParse { 11 12 public static void m

[LeetCode] NO. 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 = &qu

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 = "ab

49. Group Anagrams (string, map)

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

Total Difference String

Total Difference Strings 给一个string列表,判断有多少个不同的string,返回个数相同的定义:字符串长度相等并从左到右,或从右往左是同样的字符 abc 和 cba 为视为相同. 采用“哈希表”来存储字符串,在O(N)的时间复杂度内完成. #include <string> #include <iostream> #include <algorithm> #include <initializer_list> #include

LeetCode之389. Find the Difference

-------------------------------------------------- 先计算每个字母的出现次数然后减去,最后剩下的那一个就是后来添加的了. AC代码: public class Solution { public char findTheDifference(String s, String t) { int book[]=new int[26]; for(int i=0;i<s.length();i++) book[s.charAt(i)-'a']++; for