zoukankan      html  css  js  c++  java
  • 242. Valid Anagram

        /*
         * 242. Valid Anagram
         * 2016-6-18 by Mingyang
         * 1是别人建的bucket2是自己见得bucket,原理一样,更简洁
         * 特别注意人家的empty case一行代码就搞定,简单!
         */
         public boolean isAnagram1(String s, String t) {
                if (s == null || t == null || s.length() != t.length()) return false;
                int[] map = new int[26];
                for (int i = 0; i < s.length(); i++)
                    map[s.charAt(i) - 'a']++;
                for (int i = 0; i < t.length(); i++)
                    if(--map[t.charAt(i) - 'a'] < 0)
                        return false;
                return true;
            }
         public boolean isAnagram2(String s, String t) {
             if (s == null || t == null || s.length() != t.length()) return false;
                int[] map=new int[256];
                Arrays.fill(map,0);
                for(int i=0;i<s.length();i++){
                    map[s.charAt(i)]++;
                }
                for(int i=0;i<t.length();i++){
                    map[t.charAt(i)]--;
                    if(map[t.charAt(i)]<0)
                     return false;
                }
                return true;
            }
         /*
          * Arrays.sort(int[]) in all Java standard library implementations that I know, 
          * is an example of a comparison-based sort and thus must have worst-case complexity Ω(n log n). 
          * In particular, Oracle Java 7 uses a dual-pivot quicksort variant for the integer overloads,
          *  which actually has an Ω(n2) worst case.
          */
        public static boolean isAnagram(String a, String b) {
            char[] aa = a.toCharArray();
            char[] bb = b.toCharArray();
            Arrays.sort(aa);
            String a1 = new String(aa);
            Arrays.sort(bb);
            String b1 = new String(bb);
            if (a1.equals(b1))
                return true;
            else
                return false;
        }
  • 相关阅读:
    Java CompletableFuture:allOf等待所有异步线程任务结束
    多线程分批处理数据
    ListSplitUtil (批量数据处理)
    mysql性能优化相关
    JsonUtil
    批量数据数据处理
    多集合取交集公用方法
    ALSA:Linux下声卡编程
    word
    安卓使用自己导入的db3数据库文件
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5597357.html
Copyright © 2011-2022 走看看