设为首页 加入收藏

TOP

LN : leetcode 242 Valid Anagram
2017-10-12 12:45:26 】 浏览:10104
Tags:leetcode 242 Valid Anagram

lc 242 Valid Anagram


242 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.

analysation

利用类似于带有26个桶的数组容器存放各个字母在字符串中出现的次数。

solution

bool isAnagram(char* s, char* t) {
  int letters[26] = {0}, letters2[26] = {0};
  int i, sum = 0, num = 0;
  if (strlen(s) != strlen(t))
  return false;
  for (i = 0; i < strlen(s); i++) {
    letters[s[i]-'a']++;
    letters2[t[i]-'a']++;
    num++;
  }
  for (i = 0; i < 26; i++) {
    if (letters[i] == letters2[i])
    sum += letters[i];
  }
  return (sum == num);
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇LN : leetcode 191 Number of 1 B.. 下一篇C基础 那些年用过的奇巧淫技

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目