Hash Table

205. 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.
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        if (s.size() != t.size()) return false;
        unordered_map<char, int> s2i;
        unordered_map<char, int> t2i;

        for (size_t i = 0; i < s.size(); ++i) {
            if (s2i[s[i]] != t2i[t[i]]) return false;
            s2i[s[i]] = t2i[t[i]] = i + 1;
        }

        return true;
    }
};

290. Word Pattern

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Examples: pattern = "abba", str = "dog cat cat dog" should return true. pattern = "abba", str = "dog cat cat fish" should return false. pattern = "aaaa", str = "dog cat cat dog" should return false. pattern = "abba", str = "dog dog dog dog" should return false. Notes: You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

class Solution {
public:
    bool wordPattern(string pattern, string str) {
        unordered_map<char, string> p2s;
        unordered_map<string, char> s2p;
        vector<string> words;
        istringstream iss(str);
        string word;
        while(iss >> word) words.push_back(word);

        if (words.size() != pattern.size()) return false;
        int n = words.size();
        for (size_t i = 0; i < n; ++i) {
            if (p2s.find(pattern[i]) == p2s.end()) {
                if (s2p.find(words[i]) != s2p.end()) return false;
                p2s[pattern[i]] = words[i];
                s2p[words[i]] = pattern[i];
            } else if (pattern[i] != s2p[words[i]] || words[i] != p2s[pattern[i]]) return false;
        }

        return true;
    }
};

299. Bulls and Cows

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.

For example:

Secret number: "1807" Friend's guess: "7810" Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.) Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return "1A3B".

Please note that both secret number and friend's guess may contain duplicate digits, for example:

Secret number: "1123" Friend's guess: "0111" In this case, the 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return "1A1B". You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.

two pass solution:

class Solution {
public:
    string getHint(string secret, string guess) {
        int len1 = secret.size();
        int len2 = guess.size();
        unordered_map<char, int> candidates;
        int bull = 0;
        int cow = 0;
        int i = 0;
        for (; i < min(len1, len2); ++i) {
            if (secret[i] == guess[i]) bull++;
            else {
                // candidates.insert(secret[i]);
                candidates[secret[i]]++;
            }
        }
        while (i < len1) {
            candidates[secret[i++]]++;
        }

        for (i = 0; i < min(len1, len2); ++i) {
            if (secret[i] != guess[i] && candidates.find(guess[i]) != candidates.end()) {
                cow++;
                if (--candidates[guess[i]] == 0)
                    candidates.erase(guess[i]);
            }
        }

        return to_string(bull) + "A" + to_string(cow) + "B";
    }
};

One pass O(1) space;

class Solution {
public:
 string getHint(string secret, string guess) {
    int record[10] = {0},bulls = 0, cows = 0;
    int len = min(secret.size(),guess.size());
    for(int i = 0; i != len; ++i) {
        if(secret[i] == guess[i])
            ++bulls;
        else {
            int & decrease = record[secret[i] - '0'];
            int & increase = record[guess[i] - '0'];
            if(decrease > 0) ++cows;
            if(increase < 0) ++cows;
            --decrease;
            ++increase;
        }
    }
    return to_string(bulls)+ "A" + to_string(cows) + "B";
  }
};

results matching ""

    No results matching ""