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 ...
随机推荐
- 理解 IntelliJ IDEA 的项目配置和Web部署
1.项目配置的理解 IDEA 中最重要的各种设置项,就是这个 Project Structre 了,关乎你的项目运行,缺胳膊少腿都不行.最近公司正好也是用之前自己比较熟悉的IDEA而不是Eclipse ...
- temp-成都农商行路径
route add 30.3.4.0 mask 255.255.255.0 30.3.12.254 route add 30.3.12.0 mask 255.255.255.0 30.3.12.254 ...
- Unity Destory
Object.Destroy public static function Destroy(obj: Object, t: float = 0.0F): void; public static ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- java 线程一
java基础学习总结--线程(一) 一.线程的基本概念 线程理解:线程是一个程序里面不同的执行路径 每一个分支都叫做一个线程,main()叫做主分支,也叫主线程. 程只是一个静态的概念,机器上的一个. ...
- VCL组件之TLabel、TStaticText和TLabeledEdit
TLabel.TStaticText.TLabeledEdit类的继承关系如下:
- [mysql] ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES).
用mysql -u root -p显示ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YE ...
- 基于MyBatis3.0.6的基本操作介绍
每 一 个 MyBatis 的 应 用 程 序 都 以 一 个 SqlSessionFactory 对 象 的 实 例 为 核 心 .SqlSessionFactory本身是由SqlSessionFa ...
- JSP入门 Listener
实现HttpSessionListener 编写一个OnlineUserListener类 package anni; import java.util.List; import javax.serv ...
- 51 nod 1521 一维战舰 时间复杂度O(n),同 Codeforces 567D. One-Dimensional Battle Ships 有详细注释
题目:51nod: 题目Codeforces: 题目注意到两个战舰不能挨在一起就可以了. // 每一段 struct node{ int left; // 段的左端点 int right; // 段的 ...