zoukankan      html  css  js  c++  java
  • #Leetcode# 205. Isomorphic Strings

    https://leetcode.com/problems/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.

    Example 1:

    Input: s = "egg", t = "add"
    Output: true
    

    Example 2:

    Input: s = "foo", t = "bar"
    Output: false

    Example 3:

    Input: s = "paper", t = "title"
    Output: true

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

    代码:

    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            map<char, char> mp1, mp2;
            int ls = s.size(), lt = t.size();
            if(ls != lt) return false;
            
            for(int i = 0; i < ls; i ++) {
                if(mp1.find(s[i]) != mp1.end() && mp1[s[i]] != t[i]) return false;
                if(mp2.find(t[i]) != mp2.end() && mp2[t[i]] != s[i]) return false;
                mp1[s[i]] = t[i]; mp2[t[i]] = s[i];
            }
            return true;
        }
    };
    

      看到题目里面的映射想到了这么写 今天好像很多 map

  • 相关阅读:
    vue 之 vuex
    vue中this.$router.push() 传参
    ES6新特性
    css优先级
    创建第一个vue工程
    对Vue.js的认知
    前端的认知与见解
    Web前端常见问题
    数据库如何进行索引优化
    Python FAQ
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10044479.html
Copyright © 2011-2022 走看看