Day 20 - Valid Anagram

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~


242. Valid Anagram

Question

Given two strings s and t, return true if t is an anagram of s, and false otherwise.


Example

Example1

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

Example2

Input: s = "rat", t = "car"
Output: false

Constraints

  • 1 <= s.length, t.length <= 5 * 10^4
  • s and t consist of lowercase English letters.

Follow up:

What if the inputs contain Unicode characters? How would you adapt your solution to such a case?


解题

题目

首先先简单的翻译一下题目
给你两个字串,要判断这两个字串是不是由相同种类跟数量的字母组成。

Think

作法大致上是这样

  • Python的话,就用dictionary来存出现的字母与他对应的数量,最後再回传判断两个dictionary是否相同的布林值。
  • C的话,做法跟Python很类似,也是建两个array,这边用calloc是因为他可以将动态记忆体配置的空间的值初始化为0,如果是用malloc的话,会不知道配置空间的初始值。
  • calloc(26, sizeof(int))26代表的是连续空间的大小,跟malloc一样使用完要用free()释放掉。

Code

Python

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        dict_s = {}
        dict_t = {}
        
        for index in range(len(s)):
            if s[index] in dict_s:
                dict_s[s[index]] += 1
            else:
                dict_s[s[index]] = 1
                
        for index in range(len(t)):    
            if t[index] in dict_t:
                dict_t[t[index]] += 1
            else:
                dict_t[t[index]] = 1
                
        return dict_s == dict_t

C

ver. 1
bool isAnagram(char * s, char * t){
    // int *dict_s = malloc(sizeof (int) * 26);
    int *dict_s = calloc(26, sizeof(int));
    // int *dict_t = malloc(sizeof (int) * 26);
    int *dict_t = calloc(26, sizeof(int));

    for (int index=0 ; index<strlen(s) ; index++){
        if (dict_s[(int)(s[index])-97] != 0){
            dict_s[(int)(s[index])-97]++;
        } else {
            dict_s[(int)(s[index])-97] = 1;
        }      
    }
        
    for (int index=0 ; index<strlen(t) ; index++){
        if (dict_t[(int)(t[index])-97] != 0){
            dict_t[(int)(t[index])-97]++;
        } else {
            dict_t[(int)(t[index])-97] = 1;
        } 
            
    }
    for (int index=0 ; index<26 ; index++){
        if (dict_s[index] != dict_t[index]){
            return false;
        }
    }
    
    return true;
}
ver. 2
bool isAnagram(char * s, char * t){
    // int *dict_s = malloc(sizeof (int) * 26);
    int *dict_s = calloc(26, sizeof(int));
    // int *dict_t = malloc(sizeof (int) * 26);
    int *dict_t = calloc(26, sizeof(int));
    
    for (int index=0 ; index<strlen(s) ; index++){
        dict_s[(int)(s[index])-97]++;     
    }
        
    for (int index=0 ; index<strlen(t) ; index++){
        dict_t[(int)(t[index])-97]++;
            
    }
    for (int index=0 ; index<26 ; index++){
        if (dict_s[index] != dict_t[index]){
            return false;
        }
    }
    
    return true;
}
ver. 3
bool isAnagram(char * s, char * t){
    // int *dict_s = malloc(sizeof (int) * 26);
    int *dict = calloc(26, sizeof(int));
    
    int index_s=0, index_t=0;
    while (index_s != strlen(s) || index_t != strlen(t)){
        if (index_s<strlen(s)){
            dict[(int)(s[index_s])-97]++;
            index_s++;
        }
        if (index_t<strlen(t)){
            dict[(int)(t[index_t])-97]--;
            index_t++;
        }
    }
    
    for (int index=0 ; index<26 ; index++){
        if (dict[index] != 0){
            return false;
        }
    }
    
    return true;
}

Result

  • Python

  • C

    • ver. 1

    • ver. 2

    • ver. 3

大家明天见/images/emoticon/emoticon29.gif


<<:  Day 30:完赛感言

>>:  D-10 AoP ? autofac ? DynamicProxy

[Day3][笔记] React.js 常用 的 ES6 语法(2)

前言 延续昨天内容今天继续介绍常用 ES6 语法。 展开其余 展开运算符有几个用途 阵列 展开成个别...

IOS、Python自学心得30天 Day-21 CoreML范例

前言: 关於前面mlmodel的部分还没搞定 事先测试了一下CoreML的功能 VC程序码: imp...

Day-15 RAID

RAID tags: IT铁人 这个硬碟有多棒 在评断一个硬碟有多高的Availability时,我...

Day 28 | 来组合个画面吧 - Part 1

终於要来组合画面噜~ 写了这麽多天的小区块切版, 终於要派上用场了! 是不是常常有一种:「我想要学的...

Day02: 02 - 前端 - 开启专案、页面刻划、bootstrap-vue使用

Hi,搭给贺,我是Charlie! 在Day01当中,我们安装了所需的套件跟软件,还做了版面。 接下...