Skip to content

[LeetCode] 242. Valid Anagram #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Animenzzzz opened this issue Jul 26, 2019 · 0 comments
Open

[LeetCode] 242. Valid Anagram #10

Animenzzzz opened this issue Jul 26, 2019 · 0 comments

Comments

@Animenzzzz
Copy link
Owner

Descripe

Given two strings s and t , write a function to determine if t is an anagram of s.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: 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?

解题思路:使用哈希表,声明一个26长度的数组,初始值为0,字母每重复一次+1

C解题:

bool isAnagram(char * s, char * t){
    int len_s = strlen(s);
    int len_t = strlen(t);
    if (len_s != len_t) return false;
    
    int map[26] = {0};
    for(int i = 0;i<len_s;i++) ++map[s[i] - 'a'];
    for(int i = 0;i<len_t;i++){
        if (--map[t[i] - 'a'] < 0) return false;
    }
    return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant