242. Valid Anagram(两个字符串包含的字符是否完全相同)

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

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: 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?

解释:首先简单介绍一下Anagram(回文构词法)。Anagrams是指由颠倒字母顺序组成的单词,比如“dormitory”颠倒字母顺序会变成“dirty room”,“tea”会变成“eat”。回文构词法有一个特点:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序。

方法一:哈希表

class Solution {
    public boolean isAnagram(String s, String t) {
        if(s.toCharArray().length!=t.toCharArray().length)
            return false;
        HashMap<Character,Integer> map=new HashMap<Character,Integer>();
        for(char c:s.toCharArray()){
            if(map.containsKey(c)){
                int nums=map.get(c);
                map.put(c,nums+1);
            }else{
                map.put(c,1);
            }

        }
        for(char c:t.toCharArray()){
            if(!map.containsKey(c)){
                return false;
            }
            int nums=map.get(c);
            if(nums==1){
                map.remove(c);
            }else{
                nums--;
                map.put(c,nums);
            }

        }
        return true;
    }
}

方法二:快排

先对每个数组排序,再比较是否一样。

class Solution {
    public boolean isAnagram(String s, String t) {
        if(s.toCharArray().length!=t.toCharArray().length)
            return false;
        char[] cs=s.toCharArray();
        char[] ct=t.toCharArray();
        quickSort(cs,0,cs.length-1);
        quickSort(ct,0,ct.length-1);

        for(int i=0;i<cs.length;i++){
            if(cs[i]!=ct[i])
                return false;
        }
        return true;
    }
    private void quickSort(char[] array,int low,int high){
        int i,j;
        char t,temp;
        if(low>high) return ;
        i=low;
        j=high;
        temp=array[low];
        while(i<j){
            while(temp<=array[j]&&i<j)  j--;
            while(temp>=array[i]&&i<j)  i++;
            if(i<j){
                t=array[i];
                array[i]=array[j];
                array[j]=t;
            }
        }
        array[low]=array[j];
        array[j]=temp;
        quickSort(array,low,j-1);
        quickSort(array,j+1,high);
    }
}

原文地址:https://www.cnblogs.com/shaer/p/10846912.html

时间: 2024-10-23 03:05:48

242. Valid Anagram(两个字符串包含的字符是否完全相同)的相关文章

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

用Java编程找到两个字符串中共有的字符

这道题的算法思想是把字符串1中的每个字符与字符串2中的每个字符进行比较,遇到共同拥有的字符,放入另一个数组中,最后顺序输出即可 但是这道题的难点在于怎么排除重复的字符 public class bothChar { public static String bothChar(String str1,String str2){ StringBuffer sb = new StringBuffer(); int n1,n2,m=0; //char a[]=new char[50]; n1=str1.

[leetcode]242. Valid Anagram判断两个字符串是不是包含相同字符的重排列

/* 思路是判断26个字符在两个字符串中出现的次数是不是都一样,如果一样就返回true. 记住这个方法 */ if (s.length()!=t.length()) return false; int[] words = new int[26]; for (int i = 0; i < s.length(); i++) { words[s.charAt(i)-'a']++; words[t.charAt(i)-'a']--; } for (int i = 0; i < 26; i++) { i

Leetcode 242 Valid Anagram 字符串处理

字符串s和字符串t是否异构,就是统计两个字符串的a-z的字符数量是否一值 1 class Solution { 2 public: 3 4 bool isAnagram(string s, string t) { 5 int flgs[26] = {0};//统计s a-z的字符数量 6 for(int i = 0;i<s.size();++i){ 7 flgs[s[i] - 'a'] ++; 8 } 9 int flgt[26] = {0}; //统计t a-z的字符数量 10 for(int

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】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)统计

【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