一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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?

(二)解题

题目大意:给定两个字符串s和t,判断t是不是s的有效字谜

解题思路:有效字谜是指t是由s中的字符改变相对位置后组成的字符串。

84ms解题版本:

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.length()!=t.length()) return false;//长度不等,直接返回false
        sort(s.begin(),s.end());//排序
        sort(t.begin(),t.end());
        return s==t?true:false;//判断是否相等
    }
};

12ms的版本:

用哈希表,首先遍历s,记录每个字符出现的次数,然后遍历t,出现某个字符就次数就减1,判断最后的次数是否都为0

class Solution {
public:
    bool isAnagram(string s, string t) {
        int count[26] = {0};
        for(int i = 0 ; i < s.length();i++) count[s[i]-'a']++;
        for(int i = 0 ; i < t.length();i++) count[t[i]-'a']--;
        for(int i = 0 ; i < 26 ; i++) if(count[i]!=0) return false;
        return true;
    }
};

【一天一道LeetCode】#242. Valid Anagram的更多相关文章

  1. 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)

    22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...

  2. LN : leetcode 242 Valid Anagram

    lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...

  3. [leetcode]242. Valid Anagram验证变位词

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

  4. 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 = &q ...

  5. [LeetCode] 242. Valid Anagram 验证变位词

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

  6. LeetCode 242 Valid Anagram

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

  7. (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 = &q ...

  8. 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, ...

  9. 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 = & ...

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

随机推荐

  1. java的迭代器详解

    迭代器的引出 在jdk1.5版本之前是没有 foreach的,然而1.5版本就加上了foreach,而引入的新的foreach功能并不是在jvm上进行改进的因为代价太高,甲骨文工程师想到了一个比较好的 ...

  2. 文件服务器的详细配置之共享权限与NTFS权限的设置

       文件服务器的详细配置之共享权限与NTFS权限的设置    在大中型企业中,一般而言所谓文件服务器是指共享文件夹,即对共享权限与NTFS权限的设置!当然这也是我们搞网络者必须会的,是必经之路!我旨 ...

  3. input中v-model和value不能同时调用时解决方案

    <input type="text" v-model="keyWord" value="请输入地名地址" > 当使用如上代码时, ...

  4. Ubuntu14.04和Windows双系统时无法挂载磁盘解决方法

    基本状况:我电脑Ubuntu14.04 和 Windows10 双系统,一个固态磁盘,一个机械磁盘.Ubuntu14.04装固态里面了,固态里没有Windows内容. 问题:Ubuntu14.04系统 ...

  5. 安利三款提升幸福感的chrome插件

    谷歌访问助手 chrome浏览器一直是各大码农推荐的比较好用的浏览器,速度快.插件多. 但是由于众所周知的原因导致了谷歌账号同步.扩展商店访问慢甚至打不开的情况. 谷歌访问助手就是用来解决此问题的. ...

  6. delphi 线程教学第七节:在多个线程时空中,把各自的代码塞到一个指定的线程时空运行

    第七节:在多个线程时空中,把各自的代码塞到一个指定的线程时空运行     以 Ado 为例,常见的方法是拖一个 AdoConnection 在窗口上(或 DataModule 中), 再配合 AdoQ ...

  7. Java中的内存分配

    Java程序在运行时,需要在内存中分配空间,为了提高效率,就对空间进行了不同区域的划分,因为每一片区域否有特定的处理数据方式和内存管理方式. 1.栈存储局部变量 2.堆存储new出来的东西 3.方法区 ...

  8. Codeforces Round #305 (Div. 2) B. Mike and Fun 暴力

     B. Mike and Fun Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...

  9. Docker配置文件

    Docker 的 Registry 利用配置文件提供了一些仓库的模板(flavor),用户可以直接使用它们来进行开发或生产部署. 模板 在 config_sample.yml 文件中,可以看到一些现成 ...

  10. Rails中rspec测试xxx_path调用失败的解决

    首先要想生成类似于home_path,about_path之类的方法,必须在路由文件中添加对应方法: match '/help',to:"static_pages#help",vi ...