Re: [閒聊] 每日LeetCode已回收

看板Marginalman作者 (みけねこ的鼻屎)時間1年前 (2024/01/14 02:26), 1年前編輯推噓3(300)
留言3則, 1人參與, 1年前最新討論串601/719 (看更多)
※ 引述《JerryChungYC (JerryChung)》之銘言: : https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings- : anagram : 1347. Minimum Number of Steps to Make Two Strings Anagram : 給兩個長度相同的字串s和t,每次可以從t選一個字元換成其他字,求把t變成s的最小次數 : 只要包含的字元相同就好,順序可以不同(Anagram)。 : 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. : Python3 Code: : --------------------------------------------------- 思路: 1.因為s和t長度相同,所以s和t多的字可以拿去補少的字,找出s和t相差的數量, 除以二就好。 Java Code: ----------------------------------------- class Solution { public int minSteps(String s, String t) { int[] cnt = new int[26]; for (int i = 0; i < s.length(); i++) { cnt[s.charAt(i) - 'a']++; cnt[t.charAt(i) - 'a']--; } int res = 0; for (int c : cnt) { res += Math.abs(c); } return res/2; } } ----------------------------------------- -- https://i.imgur.com/DANRJFR.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.73.13 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1705170382.A.947.html

01/14 02:30, 1年前 , 1F
看到很多答案都這樣寫 不過窩還沒理解 :o
01/14 02:30, 1F
你畫一張表用 Example1 和 Example2 統計 s 和 t 的字元數量你就可以發現規律了 像這樣 a b s 1 2 t 2 1 i r a l e t c o d p s 1 3 1 1 1 1 t 1 1 1 1 1 2 1 t多出5個字元irapc,少了eeodl,把多的換成少的一次替換可以完成兩個字元 (5+5)/2=5

01/14 02:36, 1年前 , 2F
感覺有點懂了 謝謝捏 話說週挑戰是要報名才有的嗎
01/14 02:36, 2F
點進週賽下面可以報名然後指定時間上線打 ※ 編輯: Rushia (122.100.73.13 臺灣), 01/14/2024 02:41:55

01/14 03:06, 1年前 , 3F
為了報名去建了linkedin結果發現不用填資料也可
01/14 03:06, 3F
文章代碼(AID): #1bejNEb7 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bejNEb7 (Marginalman)