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

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?

 1 /*************************************************************************
 2     > File Name: LeetCode242.c
 3     > Author: Juntaran
 4     > Mail: [email protected]
 5     > Created Time: 2016年05月10日 星期二 03时49分39秒
 6  ************************************************************************/
 7
 8 /*************************************************************************
 9
10     Valid Anagram
11
12     Given two strings s and t, write a function to determine if t is an anagram of s.
13
14     For example,
15     s = "anagram", t = "nagaram", return true.
16     s = "rat", t = "car", return false.
17
18     Note:
19     You may assume the string contains only lowercase alphabets.
20
21     Follow up:
22     What if the inputs contain unicode characters? How would you adapt your solution to such case?
23
24  ************************************************************************/
25
26 #include "stdio.h"
27
28 int isAnagram(char* s, char* t)
29 {
30     int ret;
31
32     if (strlen(s) != strlen(t))
33     {
34         ret = 0;
35     }
36
37
38     int i;
39     int flag[26] = {0};
40
41     for ( i=0; i<strlen(s); i++ )
42     {
43         flag[s[i] - ‘a‘]++;
44         printf("%d ",s[i] - ‘a‘);
45         printf("%d\n",flag[s[i] - ‘a‘]);
46
47         flag[t[i] - ‘a‘]--;
48         printf("%d ",t[i] - ‘a‘);
49         printf("%d\n",flag[t[i] - ‘a‘]);
50     }
51
52     for( i=0; i<26; i++ )
53     {
54         if( flag[i] != 0 )
55         {
56             ret = 0;
57         }
58         printf("%d ",flag[i]);
59     }
60     ret = 1;
61     printf("\n%d\n",ret);
62
63     return ret;
64 }
65
66 int main()
67 {
68     char* s = "nl";
69     char* t = "cx";
70
71     int result = isAnagram(s,t);
72
73     return 0;
74 }
时间: 2024-10-11 13:36:26

LeetCode 242的相关文章

LeetCode 242. 有效的字母异位词(Valid Anagram)

242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram" 输出: true 示例 2: 输入: s = "rat", t = "car" 输出: false 说明: 你可以假设字符串只包含小写字母. 进阶: 如果输入字符串包含 un

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

[LeetCode]: 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. 分析: 判别“anagram”的条件: 1. 两个字符完全相等,两数完全为空:

LeetCode 242 Valid Anagram(有效字谜)(*)

翻译 给定两个字符串s和t,写一个函数来确定是否t是s的字谜. 例如, s = "anagram", t = "nagaram", 返回true s = "rat", t = "car", 返回false 备注: 你可以假设字符串只包含小写字母. 跟进: 如果输入包含unicode字符该怎么做?你的解决方案能够适应这种情况吗? 原文 Given two strings s and t, write a function to

Leetcode 242 Valid Anagram pytyhon

题目: 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. 用到了python的sorted函数,原型:sorted(data, cmp=

[LeetCode] 242. Valid Anagram Java

题目: 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. 题意及分析:要求判断两个字符串是否由相同的字符组成.这里有两种方法,(1)统计

leetcode:242 Valid Anagram-每日编程第八题

Valid Anagram Total Accepted: 42673 Total Submissions: 109227 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 true.s = "rat",

(easy)LeetCode 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. 解法:英文颠倒词,只要两个单词中,英文字母出现的次数相等即可. 代码如下: publi

【LeetCode 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. 题意: 给2个字符串,判断他们是否相等,只是顺序不一样(专业术语叫:变位词). 思路: