【Leetcode】:242. Valid Anagram问题 in Go语言
来源:互联网 发布:unity3d怎么播放动画 编辑:程序博客网 时间:2024/06/03 14:53
Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
这个问题,我的思路是用哈希表来解决,分别对s和t遍历一次,遍历s的时候,用字母当关键字,统计字母出现的次数,第二次遍历的时候,每遍历到一个字母就让这个字母的计数减一,这样就能做判断了
题目中提到了unicode的问题,在go语言中可以轻松处理,在对字符串使用for range语句的时候,提取出来的value不是byte类型而是rune类型的,一个rune就能对应一个unicode码
func isAnagram(s string, t string) bool { if (len(s) != len(t)) { return false } m := map[string]int{} for _, v := range s { key, ok := m[string(v)] if !ok { m[string(v)] = 1 } else { m[string(v)] = key + 1 } } for _, v := range t { key, ok := m[string(v)] if !ok { return false } if key == 0 { return false } m[string(v)] = key - 1 } return true}
0 0
- 【Leetcode】:242. Valid Anagram问题 in Go语言
- LeetCode 242. Valid Anagram C语言
- [LeetCode练习题-C语言]242. Valid Anagram
- [leetcode] 242.Valid Anagram
- leetcode 242. Valid Anagram
- [leetcode] 242. Valid Anagram
- leetCode 242. Valid Anagram
- Leetcode 242. Valid Anagram
- 242. Valid Anagram LeetCode
- LeetCode 242. Valid Anagram
- LeetCode 242. Valid Anagram
- [LeetCode]242. Valid Anagram
- 【LeetCode】242. Valid Anagram
- leetcode 242. Valid Anagram
- leetcode 242. Valid Anagram
- LeetCode--242. Valid Anagram
- LeetCode *** 242. Valid Anagram
- #leetcode#242. Valid Anagram
- 116. Populating Next Right Pointers in Each Node
- 实现Fragment与Activity通信
- Potato项目文档(3)
- Android之 Viewpager
- 258.[LeetCode]Add Digits
- 【Leetcode】:242. Valid Anagram问题 in Go语言
- Viewpager使用FragmentAdapter
- 类库使用记录,个人使用,以便查阅
- 自学Android开发日志
- Netty - Bytebuf(1)
- input子系统---按键程序详解
- leetCode 4. Median of Two Sorted Arrays
- APUE第一章 UNIX基础知识
- Document对象内容集合