A Leetcode a day-----Determine whether a given string conforms to a pattern

Source: Internet
Author: User
Tags bool hash
isomorphic Strings

Original title link isomorphic Strings

Given two strings to determine if one can be converted to another, the conversion rule must be the same as a pair of characters two times the conversion must be the same, if the first character ' a ' is converted to ' B ', then the next time you encounter ' a ', it can only be converted to ' B ' cannot have two characters to the same character, If the first ' a ' is converted to ' B ', then no other characters can be converted to ' B '

Ideas:

Record conversion rules through two arrays until conversion is complete

The code is as follows

Class Solution {public
:
    bool Isisomorphic (string s, String t) {
        vector<int> nums (0);
        Vector<int> count (0);
        for (int i = 0; i < s.size (); ++i) {/
            * has not been converted before, record conversion rule *
            /if (nums[s[i]] = = 0 && Count[t[i]] = = 0) {
                Nums[s[i]] = t[i];
                Count[t[i]] = 1;
            }
            /* when nums[s[i]] = = T[i] is correct, otherwise returns false *
            /else if (nums[s[i]! = T[i]) {
                return false;
            }
        }
        return true;
    }
};
Word Pattern

Original topic link Word Pattern

Determines whether a single time in a string is arranged in a given pattern

Ideas:

is still a one-to-one relationship, and similar to the above

The code is as follows

Class Solution {public
:
    bool Wordpattern (string pattern, string str) {
        Unordered_map<char, string> hash;
        Unordered_map<string, int> count;
        Std::istringstream oss (str);
        Std::string line;
        for (auto Ch:pattern) {
            std::getline (OSS, Line, ');
            if (hash.count (ch) = = 0 && count.count (line) = = 0) {
                hash[ch] = line;
                Count[line] = 1;
            }
            else if (hash[ch]! = line) {
                return false;
            }
        }
        Return Std::getline (OSS, line)? false: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.