0242.Valid-Anagram

驗證回文串

題目

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: s = "anagram", t = "nagaram" Output: true Example 2:

Input: s = "rat", t = "car" Output: false

Constraints:

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

Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

我們使用 rune 數據類型來處理 Unicode 字元,並使用 map[rune]int 來統計這些字符的出現次數。其餘的邏輯保持不變。

題目大意

解題思路

只要先把所有字符轉換成小寫,並過濾掉空格和標點這類字符,然後對剩下的字符執行 數組雙指針技巧匯總 中提到的兩端向中心的雙指針演算法即可

來源

解答

https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0242.Valid-Anagram/main.go

package validanagram

func IsAnagram(s string, t string) bool {
    if len(s) != len(t) {
        return false
    }

    record := make(map[rune]int, len(s))

    for _, v := range s {
        record[v]++
    }
    for _, v := range t {
        record[v]--
        if record[v] < 0 {
            return false
        }
    }
    return true
}

func IsAnagram2(s string, t string) bool {
    if len(s) != len(t) {
        return false
    }

    record := make(map[rune]int, len(s))

    for _, v := range s {
        record[v]++
    }
    for _, v := range t {
        record[v]--
    }

    for _, v := range record {
        if v != 0 {
            return false
        }
    }
    return true
}

Benchmark

goos: darwin
goarch: amd64
pkg: LeetcodeGolang/Leetcode/0242.Valid-Anagram
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
BenchmarkIsAnagram-8             6703497               276.3 ns/op             0 B/op          0 allocs/op
BenchmarkIsAnagram2-8            3660909               318.9 ns/op            48 B/op          2 allocs/op
PASS
ok      LeetcodeGolang/Leetcode/0242.Valid-Anagram      4.498s
© Kimi Tsai all right reserved.            Updated : 2024-05-06 09:36:37

results matching ""

    No results matching ""