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;
    }
时间: 2024-10-07 16:06:55

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

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. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. 题目大意: 判断两字符串含有的元素是否相同.

【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 example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. Note:You may assume the strin

leetcode:242 Valid Anagram-每日编程第八题

Valid Anagram Total Accepted: 42673 Total Submissions: 109227 Difficulty: Easy 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",

[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 = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. 分析: 判别“anagram”的条件: 1. 两个字符完全相等,两数完全为空:

LeetCode 242 Valid Anagram(有效字谜)(*)

翻译 给定两个字符串s和t,写一个函数来确定是否t是s的字谜. 例如, s = "anagram", t = "nagaram", 返回true s = "rat", t = "car", 返回false 备注: 你可以假设字符串只包含小写字母. 跟进: 如果输入包含unicode字符该怎么做?你的解决方案能够适应这种情况吗? 原文 Given two strings s and t, write a function to

[LeetCode] NO. 242 Valid Anagram

[题目] 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. [题目解析] 首先明确题意,需要判断组成两个字符串的所有字符是否完全相同,即

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 = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. 用到了python的sorted函数,原型:sorted(data, cmp=

【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 = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. Note:You may assume the string contains

[LeetCode] 242. Valid Anagram Java

题目: 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. 题意及分析:要求判断两个字符串是否由相同的字符组成.这里有两种方法,(1)统计