一天一道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. UDA机器学习基础—误差原因

    1.模型误差产生的原因 (1)模型无法表示基本数据的复杂度,而造成偏差. (2)因模型对训练它所用到的数据过度敏感造成的方差. 2.由偏差造成的误差--准确率和欠拟合 有足够数据表示模型,但是由于模型 ...

  2. Unity脚本自动添加注释脚本及排版格式

    Unity脚本自动添加注释脚本及头部注释排版格式 公司开发项目,需要声明版权所有,,,,标注公司名,作者,时间,项目名称及描述等等. 自己总结实现的现成脚本及头部注释排版文本,添加到模版即可. 文件不 ...

  3. 《java技术》第二次作业

    (一)学习总结 1.什么是构造方法?什么是构造方法的重载? 1)没有返回值,名字与类名相同,当新对象被创建的时候,构造函数会被调用,要想构造函数,必须声明对象并对其初始化.每一个类都有构造函数,如果没 ...

  4. 我用 tensorflow 实现的“一个神经聊天模型”:一个基于深度学习的聊天机器人

    概述 这个工作尝试重现这个论文的结果 A Neural Conversational Model (aka the Google chatbot). 它使用了循环神经网络(seq2seq 模型)来进行 ...

  5. win10利用自带的IIS搭建ftp遇到瓶颈,离线求解!!!

  6. CSS :focus 选择器

    :focus 选择器用于选取获得焦点的元素. <!DOCTYPE html> <html> <head> <style> input:focus { b ...

  7. c#默认类的修饰符。

    类的默认修饰符是internal  当其修饰符为internal时,其他模块不能访问该类, 所以class类的修饰符一般为public

  8. Linux的哲学思想

    1.一切皆文件:2.单一目的的小程序:3.组合小程序完成复杂任务:4.文本文件保存配置信息:5.尽量避免捕获用户接口:6.提供机制,而非策略. 说到底Linux的哲学思想在于方便和更好的管理后台,不同 ...

  9. Hadoop系列:(一)hdfs文件系统的基本操作

    可以执行所有常用的Linux文件操作命令(读取文件,新建文件,移动文件,删除文件,列表文件等) 1.help命令获取没个命令的帮助 [cloudera@quickstart ~]$ hadoop fs ...

  10. Python中的赋值(复制)、浅拷贝、深拷贝之间的区别

    1.赋值: 只是复制了新对象的引用,不会开辟新的内存空间.  2.浅拷贝: 创建新对象,其内容是原对象的引用.    浅拷贝有三种形式:切片操作,工厂函数,copy模块中的copy函数.    如: ...