zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    苟有恒,何必三更眠五更起;最无益,莫过一日暴十日寒。
  • 相关阅读:
    Validation failed for one or more entities. See 'EntityValidationErrors' property for more details
    Visual Studio断点调试, 无法监视变量, 提示无法计算表达式
    ASP.NET MVC中MaxLength特性设置无效
    项目从.NET 4.5迁移到.NET 4.0遇到的问题
    发布网站时应该把debug设置false
    什么时候用var关键字
    扩展方法略好于帮助方法
    在基类构造器中调用虚方法需谨慎
    ASP.NET MVC中商品模块小样
    ASP.NET MVC中实现属性和属性值的组合,即笛卡尔乘积02, 在界面实现
  • 原文地址:https://www.cnblogs.com/shaer/p/10846912.html
Copyright © 2011-2022 走看看