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;
        }
    }
  • 相关阅读:
    Chapter 4: Using Custom Property Types
    Chapter 2: Connecting to C++ Methods and Signals
    C++ Extensions: Reference examples
    qt exec
    qt component desktop
    Qt事件处理机制
    Tutorial: Writing QML Extensions with C++
    label for
    (一) HTTP 1.1支持的状态代码
    一些常用的特殊字符
  • 原文地址:https://www.cnblogs.com/hwu2014/p/4520337.html
Copyright © 2011-2022 走看看