zoukankan      html  css  js  c++  java
  • 242. 有效的字母异位词

    给定两个字符串 st ,编写一个函数来判断 t 是否是 s 的字母异位词。

    //用的是HashMap这个数据结构,和之前的那个数组中重复元素比较类似,不用这个的话,直接用散列表来看一下效果怎么样

    class Solution {
        public boolean isAnagram(String s, String t) {
            if(s.length()!=t.length())
                return false;
            Map<Character,Integer> map=new HashMap<Character,Integer>();
            for(int i=0;i<s.length();i++)
            {
                if(map.containsKey(s.charAt(i)))
                {
                    map.put(s.charAt(i),map.get(s.charAt(i))+1);
                }
                else
                    map.put(s.charAt(i),1);
            }
            for(int i=0;i<t.length();i++)
            {
                if(map.containsKey(t.charAt(i))&&map.get(t.charAt(i))>0)
                    map.put(t.charAt(i),map.get(t.charAt(i))-1);
                else
                    return false;
            }
            return true;
        }
    }

    class Solution {
        public boolean isAnagram(String s, String t) {
            if(s.length()!=t.length())
                return false;
            int[] arr=new int[26];
            for(int i=0;i<s.length();i++)
                arr[s.charAt(i)-'a']++;
             for(int i=0;i<t.length();i++)
                arr[t.charAt(i)-'a']--;
            for(int i=0;i<26;i++)
                if(arr[i]!=0)
                    return false;
            return true;
        }
    }

  • 相关阅读:
    Android Studio的project中两个build.gradle配置的区别
    build script和all projects作用和区别
    让overflow:auto页面滚动条出现时不跳动
    GreenDao设置数据版本
    Greendao 缓存问题
    js中文编码到C#后台解码
    content-type: application/json没有设置导致的500错误
    Android的Device File Explorer刷新文件
    adb server version (31) doesn't match this client (40); killing...
    Sqlserver远程过程调用失败
  • 原文地址:https://www.cnblogs.com/cold-windy/p/11312932.html
Copyright © 2011-2022 走看看