zoukankan      html  css  js  c++  java
  • Isomorphic Strings

    package cn.edu.xidian.sselab.hashtable;

    import java.util.HashMap;
    import java.util.Map;

    /**
     *
     * @author zhiyong wang
     * title:
     * content:
     * Given two strings s and t, determine if they are isomorphic.
     * Two strings are isomorphic if the characters in s can be replaced to get t.
     * All occurrences of a character must be replaced with another character while preserving the order of characters.
     *  No two characters may map to the same character but a character may map to itself.
     *  For example,
     *  Given "egg", "add", return true.
     *  Given "foo", "bar", return false.
     *  Given "paper", "title", return true.
     *  Note:
     *  You may assume both s and t have the same length.
     *
     */
    public class IsomorphicStrings {

        //自己想到的方法,注意else里面多加了一个条件,防止出现一对多的映射
        public boolean isIsomorphic(String s, String t){
            if(s == null || t == null) return false;
            int lengthS = s.length();
            int lengthT = t.length();
            if(lengthS != lengthT) return false;
            if(lengthS == 0 && lengthT != 0 || lengthS !=0 && lengthT == 0) return false;
            Map index = new HashMap();
            for(int i=0;i<lengthS;i++){
                if(index.containsKey(s.charAt(i))){
                    if(!index.get(s.charAt(i)).equals(t.charAt(i)))
                        return false;
                }else{
                    if(index.containsValue(t.charAt(i)))
                        return false;
                    index.put(s.charAt(i), t.charAt(i));
                }
            }
            return true;
        }
        
        //参考了大牛的做法,这个题与Valid Vnagram解题思路类似,但又不同,这里不是从0开始加,而是循环次数加1
         public boolean isIsomorphics(String s, String t) {
             int[] temp1 = new int[256];
             int[] temp2 = new int[256];
             int length = s.length();
             for(int i=0;i<length;i++){
                 if(temp1[s.charAt(i)] != temp2[t.charAt(i)])
                     return false;
                 temp1[s.charAt(i)] = i+1;
                 temp2[t.charAt(i)] = i+1;
             }
              return true;
            }
        
    }

  • 相关阅读:
    java的基本数据类型有八种:
    BZOJ 1003 [ZJOI2006]物流运输trans
    BZOJ 1002 [FJOI2007]轮状病毒
    BZOJ 1001 [BeiJing2006]狼抓兔子
    FFT修正
    主席树修正
    COJ 0995 WZJ的数据结构(负五)区间操作
    COJ 1010 WZJ的数据结构(十) 线段树区间操作
    COJ 0999 WZJ的数据结构(负一)
    AC自动机修正
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/5154970.html
Copyright © 2011-2022 走看看