zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    程序员学习新技术的10个建议
    ES6 let和const总结归纳
    ES6 对象扩展运算符 res运算符
    ES6 变量的解构赋值
    ES6的开发环境搭建
    vue给同一元素绑定单击click和双击事件dblclick,执行不同逻辑
    "双非"应届生校招如何获得大厂青睐?(内附前端大厂面经+技术岗超全求职攻略)
    移动端300ms与点透总结
    Web移动端适配总结
    正则表达式总结
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5479098.html
Copyright © 2011-2022 走看看