• 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;
        }
    }
    时间复杂度较高,期待更高效的方法。



  • 相关阅读:
    韦达定理
    矩阵特征值
    正交矩阵
    积分中值定理
    行列式的计算
    希尔伯特矩阵(Hilbert matrix)
    python 基于detectron或mask_rcnn的mask遮罩区域进行图片截取
    python cv2截取不规则区域图片
    python cv2读取rtsp实时码流按时生成连续视频文件
    NLP 基于kashgari和BERT实现中文命名实体识别(NER)
  • 原文地址:https://www.cnblogs.com/sunp823/p/5601436.html
Copyright © 2020-2023  润新知