{"id":9821,"date":"2022-09-17T08:32:25","date_gmt":"2022-09-17T15:32:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9821"},"modified":"2022-09-17T08:32:49","modified_gmt":"2022-09-17T15:32:49","slug":"leetcode-2399-check-distances-between-same-letters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2399-check-distances-between-same-letters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2399.\u00a0Check Distances Between Same Letters"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;string&nbsp;<code>s<\/code>&nbsp;consisting of only lowercase English letters, where each letter in&nbsp;<code>s<\/code>&nbsp;appears&nbsp;<strong>exactly<\/strong>&nbsp;<strong>twice<\/strong>. You are also given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>distance<\/code>&nbsp;of length&nbsp;<code>26<\/code>.<\/p>\n\n\n\n<p>Each letter in the alphabet is numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>25<\/code>&nbsp;(i.e.&nbsp;<code>'a' -&gt; 0<\/code>,&nbsp;<code>'b' -&gt; 1<\/code>,&nbsp;<code>'c' -&gt; 2<\/code>, &#8230; ,&nbsp;<code>'z' -&gt; 25<\/code>).<\/p>\n\n\n\n<p>In a&nbsp;<strong>well-spaced<\/strong>&nbsp;string, the number of letters between the two occurrences of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;letter is&nbsp;<code>distance[i]<\/code>. If the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;letter does not appear in&nbsp;<code>s<\/code>, then&nbsp;<code>distance[i]<\/code>&nbsp;can be&nbsp;<strong>ignored<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code><em>&nbsp;if&nbsp;<\/em><code>s<\/code><em>&nbsp;is a&nbsp;<strong>well-spaced<\/strong>&nbsp;string, otherwise return&nbsp;<\/em><code>false<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 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]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>\n- 'a' appears at indices 0 and 2 so it satisfies distance[0] = 1.\n- 'b' appears at indices 1 and 5 so it satisfies distance[1] = 3.\n- 'c' appears at indices 3 and 4 so it satisfies distance[2] = 0.\nNote that distance[3] = 5, but since 'd' does not appear in s, it can be ignored.\nReturn true because s is a well-spaced string.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 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]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong>\n- 'a' appears at indices 0 and 1 so there are zero letters between them.\nBecause distance[0] = 1, s is not a well-spaced string.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= s.length &lt;= 52<\/code><\/li><li><code>s<\/code>&nbsp;consists only of lowercase English letters.<\/li><li>Each letter appears in&nbsp;<code>s<\/code>&nbsp;exactly twice.<\/li><li><code>distance.length == 26<\/code><\/li><li><code>0 &lt;= distance[i] &lt;= 50<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Use a hastable to store the index of first occurrence of each letter.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(26)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool checkDistances(string s, vector<int>& distance) {\n    vector<int> m(26, -1);\n    for (int i = 0; i < s.length(); ++i) {\n      int c = s[i] - 'a';\n      if (m[c] >= 0 && i - m[c] - 1 != distance[c]) return false;\n      m[c] = i;\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;string&nbsp;s&nbsp;consisting of only lowercase English letters, where each letter in&nbsp;s&nbsp;appears&nbsp;exactly&nbsp;twice. You are also given a&nbsp;0-indexed&nbsp;integer array&nbsp;distance&nbsp;of length&nbsp;26. Each letter in the alphabet&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[222,82,4],"class_list":["post-9821","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9821"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9821\/revisions"}],"predecessor-version":[{"id":9823,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9821\/revisions\/9823"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}