<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [LeetCode] 1347. Minimum Number of Steps to Make Two Strings Anagram 制造字母異位詞的最小步驟數(shù)


      You are given two strings of the same length s and t. In one step you can choose any character of t and replace it with another character.

      Return the minimum number of steps to make t an anagram of s.

      An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.

      Example 1:

      Input: s = "bab", t = "aba"
      Output: 1
      Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.

      Example 2:

      Input: s = "leetcode", t = "practice"
      Output: 5
      Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.

      Example 3:

      Input: s = "anagram", t = "mangaar"
      Output: 0
      Explanation: "anagram" and "mangaar" are anagrams.

      Constraints:

      • 1 <= s.length <= 5 * 104
      • s.length == t.length
      • s and t consist of lowercase English letters only.

      這道題給了兩個字符串s和t,說是可以替換t中的字符,問最少替換多少個字符可以使得其與s是變位詞。所謂的變位詞,就是兩個單詞中字符的種類和個數(shù)均相同,就是字符的順序不同而已。之前的題目中也做過不少關(guān)于變位詞的題目,比如 Valid AnagramGroup AnagramsFind Anagram MappingsFind All Anagrams in a String 等等。這類題目的核心就是統(tǒng)計字符的出現(xiàn)次數(shù),這道題也不例外,這里使用一個 HashMap 來統(tǒng)計字符串s中每個字符的出現(xiàn)次數(shù)。然后遍歷字符串t,對于每個遍歷到的字符,將 HashMap 中該字符的映射值自減1,這樣操作之后映射值就可正可負(fù),還可能為0。當(dāng)某個映射值為正數(shù)的時候,則說明該字符在s中的數(shù)量多,若為負(fù)數(shù),則說明該字符在t中的數(shù)量多,若為0,則說明該字符在s和t中的個數(shù)一樣多。由于字符串s和t的長度相同,則正數(shù)的映射值累加和一定等于負(fù)數(shù)映射值的累加和,而且只要將所有的正數(shù)的映射字符替換成負(fù)數(shù)的映射字符,則s和t就會變成變位詞,且替換次數(shù)最少,參見代碼如下:


      class Solution {
      public:
          int minSteps(string s, string t) {
              int res = 0;
              unordered_map<char, int> charCnt;
              for (char c : s) ++charCnt[c];
              for (char c : t) --charCnt[c];
              for (auto a : charCnt) {
                  if (a.second > 0) res += abs(a.second);
              }
              return res;
          }
      };
      

      Github 同步地址:

      https://github.com/grandyang/leetcode/issues/1347


      類似題目:

      Valid Anagram

      Group Anagrams

      Find Anagram Mappings

      Find All Anagrams in a String

      Determine if Two Strings Are Close

      Minimum Number of Steps to Make Two Strings Anagram II


      參考資料:

      https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/

      https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/solutions/503460/java-maintain-an-array-to-record-the-occurrence-of-characters/

      https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/solutions/503450/java-python-3-count-occurrences-and-sum-the-difference-w-analysis/


      LeetCode All in One 題目講解匯總(持續(xù)更新中...)

      posted @ 2023-06-05 23:44  Grandyang  閱讀(266)  評論(0)    收藏  舉報
      Fork me on GitHub
      主站蜘蛛池模板: 日韩深夜视频在线观看| 精品麻豆国产色欲色欲色欲WWW | 天天做天天爱夜夜爽| 国产不卡免费一区二区| √天堂中文www官网在线| 成年女人免费碰碰视频| 精品国产成人亚洲午夜福利| 成人免费看片又大又黄| 蜜臀精品视频一区二区三区| 亚洲成av人片色午夜乱码| 麻豆精品一区二区三区蜜臀| 国产热A欧美热A在线视频| 97精品国产91久久久久久久| 国产中文99视频在线观看| 成人午夜电影福利免费| 日本亚洲一区二区精品久久| 欧美性猛交xxxx乱大交丰满| 亚洲综合在线一区二区三区| 亚洲精品国模一区二区| 一本色道久久综合熟妇人妻| 国产AV福利第一精品| 日韩中文字幕高清有码| 国产福利深夜在线观看| 国模在线视频一区二区三区 | 日日猛噜噜狠狠扒开双腿小说| 玩弄美艳馊子高潮无码| 日韩V欧美V中文在线| 革吉县| 国产91麻豆精品成人区| 阳西县| 精品无人区一码二码三码| 国产99青青成人A在线| 又爽又黄又无遮挡的激情视频| 国产在线精品欧美日韩电影| 国产精品国语对白露脸在线播放| 国产熟女一区二区三区四区| 久久综合色之久久综合| 久久91精品牛牛| 日本www一道久久久免费| 亚洲自在精品网久久一区| 扒开女人内裤猛进猛出免费视频 |