面試題 16.18. Pattern Matching LCCI(Leetcode每日一題-2020.06.22)

Problem

You are given two strings, pattern and value. The pattern string consists of just the letters a and b, describing a pattern within a string. For example, the string catcatgocatgo matches the pattern aabab (where cat is a and go is b). It also matches patterns like a, ab, and b. Write a method to determine if value matches pattern. a and b cannot be the same string.

Note:

  • 0 <= len(pattern) <= 1000
  • 0 <= len(value) <= 1000
  • pattern only contains “a” and “b”, value only contains lowercase letters.

Example1

Input: pattern = “abba”, value = “dogcatcatdog”
Output: true

Example2

Input: pattern = “abba”, value = “dogcatcatfish”
Output: false

Example3

Input: pattern = “aaaa”, value = “dogcatcatdog”
Output: false

Example4

Input: pattern = “abba”, value = “dogdogdogdog”
Output: true
Explanation: “a”=“dogdog”,b="",vice versa.

Solution

class Solution {
public:
    bool patternMatching(string pattern, string value) {
        if(pattern.empty())
            return value.empty();
        //if(value.empty())
            //return false;

        char mainChar = pattern[0];
        char altChar = mainChar == 'a' ? 'b':'a';

        int val_length = value.length();

        int mainChar_cnt = countOf(pattern,mainChar);
        int altChar_cnt = pattern.length() - mainChar_cnt;
        int first_altChar = pattern.find_first_of(altChar);
        int max_mainStr_size = val_length / mainChar_cnt;

        for(int mainStr_size = 0;mainStr_size <=max_mainStr_size;++mainStr_size)
        {
            int remaining_len = val_length - mainStr_size * mainChar_cnt;
            string mainStr = value.substr(0,mainStr_size);
            if(altChar_cnt == 0 || remaining_len % altChar_cnt == 0)
            {
                int alt_idx = first_altChar * mainStr_size;
                int altStrSize = altChar_cnt == 0?0:remaining_len / altChar_cnt;
                string altStr = altChar_cnt==0?"":value.substr(alt_idx,altStrSize);

                 
                if(mainStr.empty() && altChar_cnt == 0 && value.empty()) //處理特例 "a",""
                    return true;

                if(mainStr.empty() && altStr.empty() && value.empty()) //處理特例 "ab",""
                    return false;
                
                if(isMatch(pattern,mainStr,altStr,value))
                    return true;
            }
        }

        return false;
    }

    int countOf(const string &pattern,char ch)
    {
        int cnt = 0;
        for(auto c:pattern)
        {
            if(c == ch)
                ++cnt;
        }

        return cnt;
    }

    bool isMatch(const string &pattern,const string &mainStr,const string &altStr,const string&value)
    {
        string tmp = "";
        for(auto c:pattern)
        {
            if(c == pattern[0])
                tmp += mainStr;
            else
                tmp += altStr;
        }

        return tmp == value;
    }

        
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章