首页 > 其他分享 >leetcode-2399-easy

leetcode-2399-easy

时间:2022-11-29 21:58:34浏览次数:56  
标签:key distance return string map appears easy leetcode 2399

Check Distances Between Same Letters

You are given a 0-indexed string s consisting of only lowercase English letters, where each letter in s appears exactly twice. You are also given a 0-indexed integer array distance of length 26.

Each letter in the alphabet is numbered from 0 to 25 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, ... , 'z' -> 25).

In a well-spaced string, the number of letters between the two occurrences of the ith letter is distance[i]. If the ith letter does not appear in s, then distance[i] can be ignored.

Return true if s is a well-spaced string, otherwise return false.

Example 1:

Input: s = "abaccb", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: true
Explanation:
- 'a' appears at indices 0 and 2 so it satisfies distance[0] = 1.
- 'b' appears at indices 1 and 5 so it satisfies distance[1] = 3.
- 'c' appears at indices 3 and 4 so it satisfies distance[2] = 0.
Note that distance[3] = 5, but since 'd' does not appear in s, it can be ignored.
Return true because s is a well-spaced string.
Example 2:

Input: s = "aa", distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: false
Explanation:
- 'a' appears at indices 0 and 1 so there are zero letters between them.
Because distance[0] = 1, s is not a well-spaced string.
Constraints:

2 <= s.length <= 52
s consists only of lowercase English letters.
Each letter appears in s exactly twice.
distance.length == 26
0 <= distance[i] <= 50

思路一:遍历字符,记录每个字符的位置,然后对比给定的数组是否匹配

public boolean checkDistances(String s, int[] distance) {
    Map<Character, Integer> map = new HashMap<>();

    for (int i = 0; i < s.length(); i++) {
        int finalI = i;
        map.compute(s.charAt(i), (k, v) -> v == null ? finalI : finalI - v - 1);
    }

    for (Map.Entry<Character, Integer> e : map.entrySet()) {
        Character c = e.getKey();
        if (distance[c - 'a'] != e.getValue()) return false;
    }

    return true;
}

思路二:优化思路一,在遍历字符时就可以提前判断字符位置是否正确

public boolean checkDistances(String s, int[] distance) {
    Map<Character, Integer> map = new HashMap<>();

    for (int i = 0; i < s.length(); i++) {
        char key = s.charAt(i);
        if (map.containsKey(key)) {
            if (i - map.get(key) != distance[key - 'a']) return false;
        } else {
            map.put(key, i);
        }
    }

    return true;
}

标签:key,distance,return,string,map,appears,easy,leetcode,2399
From: https://www.cnblogs.com/iyiluo/p/16936825.html

相关文章

  • leetcode-917-easy
    ReverseOnlyLettersGivenastrings,reversethestringaccordingtothefollowingrules:AllthecharactersthatarenotEnglishlettersremaininthesame......
  • leetcode-119-easy
    Pascal'sTriangleIIGivenanintegerrowIndex,returntherowIndexth(0-indexed)rowofthePascal'striangle.InPascal'striangle,eachnumberisthesumo......
  • leetcode-112-easy
    PathSumGiventherootofabinarytreeandanintegertargetSum,returntrueifthetreehasaroot-to-leafpathsuchthataddingupallthevaluesalongthe......
  • leetcode-628-easy
    MaximumProductofThreeNumbersGivenanintegerarraynums,findthreenumberswhoseproductismaximumandreturnthemaximumproduct.Example1:Input:n......
  • leetcode-1399-easy
    CountLargestGroupYouaregivenanintegern.Eachnumberfrom1tonisgroupedaccordingtothesumofitsdigits.Returnthenumberofgroupsthathave......
  • 力扣 leetcode 45. 跳跃游戏 II
    问题描述给你一个非负整数数组nums,你最初位于数组的第一个位置。数组中的每个元素代表你在该位置可以跳跃的最大长度。你的目标是使用最少的跳跃次数到达数组的最后一......
  • leetcode 1976.到达目的地的方案数
    问题描述:你在一个城市里,城市由n 个路口组成,路口编号为 0 到 n-1 ,某些路口之间有双向 道路。输入保证你可以从任意路口出发到达其他任意路口,且任意两个路口之间......
  • 力扣 leetcode 1758. 生成交替二进制字符串的最少操作数
    问题描述给你一个仅由字符'0'和'1'组成的字符串s。一步操作中,你可以将任一'0'变成'1',或者将'1'变成'0'。交替字符串定义为:如果字符串中不存在相邻两个字......
  • 对比山海鲸可视化跟EasyV,你更看好谁?
    现代的数据可视化设计一般喜欢追求更加高效的工具,我们在选择可视化工具的时候,一定会被繁多的可视化产品晃得眼花缭乱。今天给大家推荐2款我用过的可视化软件,不谈缺陷,只看优......
  • [LeetCode] 1758. Minimum Changes To Make Alternating Binary String
    Youaregivenastring s consistingonlyofthecharacters '0' and '1'.Inoneoperation,youcanchangeany '0' to '1' orviceversa.Thestringisc......