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. IDEA配置Tomcat

    如何用IDEA写Servlet在我别的博文有! 注意:如果不能成功启动Tomcat,很有可能是JDK版本和Tomcat版本不匹配,此时你可以降低JDK版本试试

  2. Oracle 修改序列的初始值

    Oracle 序列(Sequence)主要用于生成主键.但是,有时需要修改序列初始值(START WITH)时,好多人凭感觉认为:Alter Sequence SequenceName Start W ...

  3. OSGi-入门篇之生命周期层(03)

    前言 生命周期层在OSGi框架中属于模块层上面的一层,它的运作是建立在模块层的功能之上的.生命周期层一个主要的功能就是让你能够从外部管理应用或者建立能够自我管理的应用(或者两者的结合),并且给了应用本 ...

  4. 初入ubuntu

    登入root :su root 安装 vim: sudo apt-get install vim 安装 gcc(g++):sudo apt-get install gcc(g++) 非常实用的修改分辨 ...

  5. 在Storyboard中为UITableView添加Header和Footer

    我在这里所说的Header和Footer并不是sectionHeader和sectionFooter,而是指UITableView的tableHeaderView和tableFooterView,这两 ...

  6. java集合系列——Set之HashSet和TreeSet介绍(十)

    一.Set的简介 Set是一个不包含重复元素的 collection.更确切地讲,set 不包含满足 e1.equals(e2) 的元素.对 e1 和 e2,并且最多包含一个为 null 的元素. S ...

  7. Stochastic Gradient Descent

    一.从Multinomial Logistic模型说起 1.Multinomial Logistic 令为维输入向量; 为输出label;(一共k类); 为模型参数向量: Multinomial Lo ...

  8. IFrame父页面和子页面的交互

    现在在页面里面用到iframe的情况越来越少了,但有时还是避免不了,甚至这些页面之间还需要用js来做交互,那么这些页面如何操作彼此的dom呢?下面将会逐步介绍. 1.父页面操作子页面里面的dom 下面 ...

  9. 550 Create directory operation failed

    往Linux系统中上传文件时候,我们经常会使用FTP连接Linux,也经常会使用mkdir命令来创建目录.最近发现用mkdir创建目录时提示550 Create directory operation ...

  10. zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~

    Intervals Time Limit: 1 Second      Memory Limit:65536 KB      Special Judge Chiaki has n intervals ...