LeetCode 205 Isomorphic Strings (homogeneous string) (string, vector, map )(*)

Source: Internet
Author: User

LeetCode 205 Isomorphic Strings (homogeneous string) (string, vector, map )(*)
Translation

Given two strings s and t, determine whether they are homogeneous. If the elements in s are replaced with t, the two strings are homogeneous. The sequence of all characters must be retained when an element of a string is replaced by another element. No two characters can be mapped to the same character, but the characters can be mapped to the character itself. For example, if "egg" and "add" are specified, the return value is true. Given "foo" and "bar", false is returned. Given "paper" and "title", return true. Note: Assume that s and t have the same length.
Original
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.
Analysis

After translating this question, I naturally think of a method. I want to output all the strings into numerical sequences:

For example,Given "paper", return "01023".Given "foo", return "011".Given "isomorphic", return "0123245607".

So we implemented this function:

vector
  
    getVecOrder(string str) {    map
   
     strM;    int index = 0;    vector
    
      strVec;    for (int i = 0; i < str.size(); ++i) {        auto iter = strM.find(str[i]);        if (iter == strM.end()) {            strM.insert(pair
     
      (str[i], index));            strVec.push_back(index);            index += 1;        }        else {            strVec.push_back(strM[str[i]]);        }    }    return strVec;}
     
    
   
  

Here, map is used to save the key-value pairs of each character and index. The index is represented by index, and the index starts from 0.

The final numeric sequence is saved by vector.

Cyclically traverses the entire string and finds a character in the map. If no character is found, add the character and the corresponding index. If the character already exists, the index of the character is obtained from the map and added to the vector.

With this module function, it is easy to solve the problem:

bool isIsomorphic(string s, string t) {    vector
  
    v_s = getVecOrder(s), v_t = getVecOrder(t);    for (int i = 0; i < v_s.size(); ++i) {        if (v_s[i] != v_t[i]) return false;    }                                                      return true;}
  

Because the length of the string is equivalent, the length of the vector must be equal.

Code
class Solution {public:    vector
  
    getVecOrder(string str) {        int len = str.size();        map
   
     strM;        int index = 0;        vector
    
      strVec;        for (int i = 0; i < len; ++i) {            auto iter = strM.find(str[i]);            if (iter == strM.end()) {                strM.insert(pair
     
      (str[i], index));                strVec.push_back(index);                index += 1;            }            else {                strVec.push_back(strM[str[i]]);            }        }        return strVec;    }    bool isIsomorphic(string s, string t) {        vector
      
        v_s = getVecOrder(s), v_t = getVecOrder(t); for (int i = 0; i < v_s.size(); ++i) { if (v_s[i] != v_t[i]) return false; } return true; }};
      
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.