zoukankan      html  css  js  c++  java
  • LeetCode_Isomorphic Strings

    Isomorphic 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.

    Note:
    You may assume both s and t have the same length.

    判断两个字符串是否同构:s中的每个字符按顺序映射到t中一个字符,映射关系不能是一对多,例如"foo"和"bar",按顺序映射:f->b,o->a,o->r,o同时对a和r,所以foo和egg不是同构的;同理,不能多对一,即s中不同字符不能对应t中相同的字符,例如,"ab"和"aa",a->a,b->a,a和b同时对应a,所以ab和aa不符合条件。

    以s中的每个字符作为key,t中的每个字符作为value,利用Java中的map容器做映射。

    public class Solution {
        public boolean isIsomorphic(String s, String t) {
        Map<Character,Character> mp1 = new HashMap<>();
    		final int len1 = s.length();
    		final int len2 = s.length();
    		if(len1!=len2) return false;
    		if(len1==0) return true;
    		for(int i = 0;i < len1;i++)
    		{
    			if(!mp1.containsKey(s.charAt(i)))
    			{
    				mp1.put(s.charAt(i),t.charAt(i));
    				for(int j = 0;j<i;j++)
    				{
    					if(mp1.get(s.charAt(i))==mp1.get(s.charAt(j)))//多对一
    					{
    						return false;
    					}
    				}
    			}
    			else
    			{
    				if(mp1.get(s.charAt(i))!=t.charAt(i))//一对多
    				{
    					return false;
    				}
    			}
    		}
            return true;
        }
    }
    时间复杂度较高,期待更高效的方法。



  • 相关阅读:
    FLEX图像工具类-图像变形
    flex中list或Combox中的子项上移下移操作
    flex中socket的使用
    Flex2款简单FLV播放器很经典
    Flex中Css参考示例
    Flex中CursorManager的应用
    关于FLEX的安全沙箱问题
    Flex实现多文件上传之一:前台部分
    Flex与JS通信
    metasploit 常用指令
  • 原文地址:https://www.cnblogs.com/sunp823/p/5601436.html
Copyright © 2011-2022 走看看