• 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: Jacinthmail@gmail.com
     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
    ",flag[s[i] - 'a']);
    46 
    47         flag[t[i] - 'a']--;
    48         printf("%d ",t[i] - 'a');
    49         printf("%d
    ",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("
    %d
    ",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 }
  • 相关阅读:
    SQL 存储过程入门(五)
    QT程序崩溃原因示例和分析
    全志(allwinner)编译过程问题与解决方法汇总
    rust Linux环境下开发环境安装 和 IDE工具 EMACS配置
    DMZ
    什么是浮动IP
    PLMN和PSTN
    人生 -- 有感于近期的一个事件
    职场四原则
    Fatal error compiling: java.lang.NoSuc hFieldError??
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5479098.html
Copyright © 2020-2023  润新知