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://最容易想到的是,两个循环嵌套,把字符串s拆成一个一个字符拿去和字符串t比较,若t中有,则剔除掉该字符。(时间复杂度为O(n^2))不推荐
    思路2://更简单的是,使用java内置的排序,然后判断字符串是否相等,也比较慢
    思路3://想多一步,用一个数组来记录出现的次数,可以使时间复杂度达到O(n)

思路1比较简单,就不做过多的阐述。

思路2:先利用内置排序,使得字符数组有序,然后合成两个新的字符串,然后判断这两个新的字符串是否相等,即可判断是否是Anagram

代码如下:

    public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()) return false;
if(s.length()==0&&t.length()==0) return true;
String [] sa=new String[s.length()];
String [] ta=new String[s.length()];
for(int i=0;i<s.length();i++)
{
sa[i]=s.substring(i,i+1);
ta[i]=t.substring(i,i+1);
}
Arrays.sort(sa);
Arrays.sort(ta);
s=null;
t=null;
for(int i=0;i<sa.length;i++)
{
s=sa[i]+s;
t=ta[i]+t;
}
if(s.equals(t))
return true;
return false; }

思路3:

声明两个26个单位的数组,当字母出现的时候,相应数组位置加一,最后判断相应位置是否等值,即可判断出结果。

 public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()) return false;
int[] team1=new int[26];
int[] team2=new int[26];
char[] cs=s.toCharArray();
char[] ct=t.toCharArray();
for(int i=0;i<s.length();i++)
{
team1[cs[i]-'a']+=1;
team2[ct[i]-'a']+=1;
}
for(int i=0;i<26;i++)
if(team1[i]!=team2[i])
return false;
return true;
}

思路3的变体:

在s中出现的字符就在相应的位置加一,在t中出现的字符就在相应的位置减一,最后判断结果是否为0就可以判断是否是Anagram,这样可以省去一个数组的空间。

  public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()) return false;
int[] team=new int[26];
char[] cs=s.toCharArray();
char[] ct=t.toCharArray();
for(int i=0;i<s.length();i++)
team[cs[i]-'a']+=1;
for(int i=0;i<s.length();i++)
team[ct[i]-'a']-=1;
for(int i=0;i<26;i++)
if(team[i]!=0)
return false;
return true;
}

242. Valid Anagram(leetcode)的更多相关文章

  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. 242. Valid Anagram(C++)

    242. Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. ...

  4. 【LeetCode】242. Valid Anagram (2 solutions)

    Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For ...

  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

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  7. 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. [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: ...

  9. 【LeetCode】242. Valid Anagram 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 字典统计词频 排序 日期 [LeetCode] 题目地址:ht ...

随机推荐

  1. Java课程设计---团队博客

    课设题目:购物车系统 题目要求: 1.先建立一个文本文件,定义出自己想要的商品.//也可用数据库以商品编号:商品名称:商品品牌:价格作为文件的内容,中间要求用分号或者分割 2.编写程序,定义一个商品类 ...

  2. 201521123062《Java程序设计》第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  3. 201521123070 《JAVA程序设计》第14周学习总结

    1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 Q1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现 ...

  4. Activiti第二篇【管理流程定义、执行任务和流程实例、流程变量】

    上篇Activiti只是一个快速入门案例,这篇就讲定义.部署.查看任务等等的一些细节[涉及到的数据库表.对象等等]- 管理流程定义 管理流程定义主要涉及到以下的4张表: -- 流程部署相关的表 SEL ...

  5. Hibernate第四篇【集合映射、一对多和多对一】

    前言 前面的我们使用的是一个表的操作,但我们实际的开发中不可能只使用一个表的-因此,本博文主要讲解关联映射 集合映射 需求分析:当用户购买商品,用户可能有多个地址. 数据库表 我们一般如下图一样设计数 ...

  6. Jmeter运行结果分析

    1.聚合报告 以上就是我们所关心的结果 Label:页面或请求名称 Samples:运行的线程数(也可理解为请求数) Average:平均响应时间 Median:响应时间的中值 90% Line:90 ...

  7. 第6章 Overlapped I/O, 在你身后变戏法 ---1

    这一章描述如何使用 overlapped I/O(也就是 asynchronous I/O).某些时候 overlapped I/O 可以取代多线程的功用.然而,overlapped I/O 加上co ...

  8. 【转】css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?

    摘要: css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?     一.抛一块问题砖(display: block)先看现象: 分析HTML代码结构: <div class ...

  9. Json操作问题总结

    大家都知道,Json是一种轻量级的数据交换格式,对JS处理数据来说是很理想滴! 熟练写过xxx.json文件和操作的小伙伴来说,我说的问题都不是什么大问题啦,可以忽略本宝宝的文章,更希望各位大佬指点一 ...

  10. Painter's Problem poj1681 高斯消元法

    Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4420   Accepted: 2143 ...