242. Valid Anagram(leetcode)
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)的更多相关文章
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- 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 ...
- 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. ...
- 【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 ...
- [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: ...
- 【一天一道LeetCode】#242. Valid Anagram
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 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 ...
- [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: ...
- 【LeetCode】242. Valid Anagram 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 字典统计词频 排序 日期 [LeetCode] 题目地址:ht ...
随机推荐
- 201521123048 《Java程序设计》第9周学习总结
1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? ...
- 单例模式(Singleton)看了就懂
单例,故名思议,一个只能创建一个实例的类. 单例被广泛应用于Spring的bean(默认).线程池.数据库连接池.缓存,还有其他一些无状态的类如servlet. 一个没必要多例的类实现了单例可以节约空 ...
- SpringMVC HelloWorld实例开发及部署
SpringMVC HelloWorld实例开发及部署 2017-01-24 目录 1 Tomcat及Eclipse Tomcat插件安装配置 1.1 Tomcat的安装 1.2 Eclipse ...
- HDU 6092`Rikka with Subset 01背包变形
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- Python之面向对象与类
本节内容 面向对象的概念 类的封装 类的继承 类的多态 静态方法.类方法 和 属性方法 类的特殊成员方法 子类属性查找顺序 一.面向对象的概念 1. "面向对象(OOP)"是什么? ...
- 初学node.js有感二
node.js进阶 一.回顾与继续 对于一种语言的认识都是经历这样的一个过程的,首先从原生的环境(CMD)中开始学习,找到一门语言之间各种引用的本质和相互之间的调用方式,明澈各种依赖关系,在这个基 ...
- linux cpu load学习笔记
linux系统中的Load对当前CPU工作量的度量 Load Average 就是一段时间 (1 分钟.5分钟.15分钟) 内平均Load. [root@CNC-BJ-5-3N1 ~]# w 20:0 ...
- UI自动化测试(三)对页面中定位到的元素对象做相应操作
前两天分别讲述了UI自动化测试基础以及对页面元素该如何进行定位,这一篇自然就是对定位到的页面元素对象进行相应操作啦. 阅读目录 1.常用操作元素对象的方法 2.鼠标事件操作 3.键盘事件操作 4.We ...
- (译)通过 HTML、JS 和 Electron 创建你的第一个桌面应用
原文:Creating Your First Desktop App With HTML, JS and Electron 作者:Danny Markov 近年来 web 应用变得越来越强大,但是桌面 ...
- git添加比较和合并工具(meld)
git 下的(difftool)和(mergetool)是专门提供给使用者用自己的工具进行diff和merge的命令: # git config --global diff.tool meld # g ...