isAnagram

/*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 static boolean isAnagram(String s, String t) {
        int[] ch1 = new int[26];
        char[] cha1 = s.toCharArray();
        char[] cha2 = t.toCharArray();
        if (cha1.length != cha2.length)
            return false;
        for (int i = 0; i < cha1.length; i++) {
            int temp = cha1[i] - 97;
            ch1[temp]++;
        }
        for (int i = 0; i < cha2.length; i++) {
            int temp = cha2[i] - 97;
            ch1[temp]--;
        }
        for (int i : ch1) {
            if (i != 0)
                return false;
        }
        return true;
    }
}
时间: 2024-10-08 02:35:18

isAnagram的相关文章

Valid Anagram

题目链接:https://leetcode.com/problems/valid-anagram/ 题目大意:就是判断两个字符串是否由相同的字符组成(字符个数也要相同) 1)排序方法 将字符串进行排序以后,如果想等,即可 代码如下: class Solution { public: bool isAnagram(string s, string t) { string s_t = s; string t_t = t; sort(s_t.begin(), s_t.end()); sort(t_t.

Lettocde_242_Valid Anagram

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48979767 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"

哈希的妙用

假设须要推断多个字符是不是在某个字符串里面出现过或者统计多个字符在某个字符串中出现的次数,我们能够考虑基于数组创建一个简单的hash表,这样能够用非常小的空间消耗来换取时间效率的提升. 题目1:从第一个字符串中删除第二个字符串中出现的全部字符 思路:准备一个hash数组.遍历第二个串,并以每一个字符所相应的asc码作为下标,值为是否出现,1代表出现.然后遍历第一个串.每遍历一个字符,就查询hash表(O(1)),若值为1.那么删除该字符. /* 从第一个字符串中删除第二个字符串中出现的全部字符

242.判断一个字符串是否为另一个的乱序 Valid Anagram

错误1 "aa" "bb" static public bool IsAnagram(string s, string t) { int sLength = s.Length; int tLength = t.Length; if (sLength != tLength) { return false; } char c = ' '; int value = 0; Dictionary<char, int> d = new Dictionary<c

Leetcode 242 Valid Anagram 字符串处理

字符串s和字符串t是否异构,就是统计两个字符串的a-z的字符数量是否一值 1 class Solution { 2 public: 3 4 bool isAnagram(string s, string t) { 5 int flgs[26] = {0};//统计s a-z的字符数量 6 for(int i = 0;i<s.size();++i){ 7 flgs[s[i] - 'a'] ++; 8 } 9 int flgt[26] = {0}; //统计t a-z的字符数量 10 for(int

【09_242】Valid Anagram

Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Easy 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 t

leetcdoe Valid Anagram

题目连接 https://leetcode.com/problems/valid-anagram/ Valid Anagram Description 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.

LeetCode Valid Anagram (简单题)

题意: 给出两个字符串s和t,判断串t是否为s打乱后的串. 思路: 如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样.那么可以统计26个字母的次数来解决,复杂度O(n).也可以排序后逐个比对,复杂度O(nlogn). 第一种方法: 1 class Solution { 2 public: 3 bool isAnagram(string s,string t) 4 { 5 if(s.size()!=t.size()) return false; 6 int cnt[26][2]

【ACM从零开始】LeetCode OJ-Valid Anagram

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 onl