242. Valid Anagram

来源:互联网 发布:淘宝上钻石多少好评 编辑:程序博客网 时间:2024/06/17 01:39

原题

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.

代码实现

    public bool IsAnagram(string s, string t) {        int[] hash = new int[123]; //a~z        if(s.Length!=t.Length) return false;        foreach(var ch in s){            hash[Convert.ToInt32(ch)]++;        }        foreach(var ch in t){           if(hash[Convert.ToInt32(ch)]--<=0)              return false;        }        return true;    }

leetcode-solution库

leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp

2 0
原创粉丝点击