LeetCode之有效的字母異位詞

2021-09-02 18:32:11 字數 1026 閱讀 2248

介紹:給定兩個字串 s 和 t ,編寫乙個函式來判斷 t 是否是 s 的乙個字母異位詞。

示例 1:

輸入: s = "anagram", t = "nagaram"

輸出: true

示例2:

輸入: s = "rat", t = "car"

輸出: false

說明: 你可以假設字串只包含小寫字母。

高階:如果輸入字串包含 unicode 字元怎麼辦?你能否調整你的解法來應對這種情況?

源**

#include#include#include#include#include #includeusing namespace std;

bool isanagram(string s, string t)

for (int j = 0; j < t.length(); j++)

map::iterator it_s = mapset_s.begin();

map::iterator it_t = mapset_t.begin();

if (mapset_s.size() == mapset_t.size())

}} if (k == mapset_s.size())

else }

int main()

除了c++源**,這裡還有乙份python源**,實現起來特別簡單。

from collections import counter

class solution:

def isanagram(self, s, t):

""":type s: str

:type t: str

:rtype: bool

"""c1 = counter(s)

c2 = counter(t)

if (c1 == c2):

return true

return false

LeetCode刷題之242 有效的字母異位詞

我不知道將去向何方,但我已在路上!示例 1 輸入 s anagram t nagaram 輸出 true示例 2 輸入 s rat t car 輸出 falseclass solution def isanagram self,s str,t str bool if len s len t retu...

三 leetcode之有效的括號

題目描述 給定乙個只包括 的字串,判斷字串是否有效。有效字串需滿足 左括號必須用相同型別的右括號閉合。左括號必須以正確的順序閉合。注意空字串可被認為是有效字串。示例 1 輸入 輸出 true 示例 2 輸入 輸出 true 示例 3 輸入 輸出 false 示例 4 輸入 輸出 false 示例 5...

排序題目之有效的字母異位詞

題目 給定兩個字串 s 和 t,編寫乙個函式來判斷 t 是否是 s 的字母異位詞 python語言 1 示例 1 23 輸入 s anagram t nagaram 4 輸出 true 5 示例 2 67 輸入 s rat t car 8 輸出 false 說明 你可以假設字串只包含小寫字母。高階 ...