zoukankan      html  css  js  c++  java
  • 205-Ismorphics Strings

    【题目】

    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.

      给定2个字符串s和t,判断他们是否是同构。

      如果在s中的所有字符可以被t中的字符替换,则两个字符串是同构的。

      所有出现的字符必须被另一个字符替换,没有2个字符可以映射到同一个字符上

      举例

      给 "egg" , "add" , return true.

      给 "foo" , "bar" , return false.

      给 "paper" , "title" , return true.

    【分析】

      1. 将两个字符串的字符映射到map上

    【算法实现】

    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            Map<Character,Character> map = new HashMap<Character,Character>();
            //Map<Character,Character> map2 = new HashMap<Character,Character>();
            char[] cs = s.toCharArray();
            char[] ts = t.toCharArray();
            int len = cs.length;
            for(int i=0; i<len; i++) {
                if(map.containsKey(cs[i])) {
                    if(map.get(cs[i])!=ts[i])
                        return false;
                }else if(map.containsValue(ts[i])){
                    return false;
                }else {
                    map.put(cs[i], ts[i]);
                }
            }
            return true;
        }
    }
  • 相关阅读:
    队列
    使用JPype实现Python调用JAVA程序
    Django和Flask对于URL尾斜杠(back slash)的处理
    数据仓库建设中的数据建模方法(转)
    python自定义logger handler
    Eclipse下.project和.classpath作用(转)
    理解python的with语句
    django常见小问题收集(转)
    windows下无法创建django工程的问题
    Excel的python读写
  • 原文地址:https://www.cnblogs.com/hwu2014/p/4520337.html
Copyright © 2011-2022 走看看