Programmer Interview Gold Code-Interview Question 16.18. Pattern Matching (Logical Questions)

1. Title

You have two strings, pattern and value.
The pattern string consists of the letters "a" and "b" and is used to describe the pattern in the string.
For example, the string "catcatgocatgo" matches the pattern "aabab" (where "cat" is "a" and "go" is "b"), and the string also matches things like "a", "ab", and "b" mode.
But please note that "a" and "b" cannot mean the same string at the same time .
Write a method to determine whether the value string matches the pattern string.

示例 1:
输入: pattern = "abba", value = "dogcatcatdog"
输出: true

示例 2:
输入: pattern = "abba", value = "dogcatcatfish"
输出: false

示例 3:
输入: pattern = "aaaa", value = "dogcatcatdog"
输出: false

示例 4:
输入: pattern = "abba", value = "dogdogdogdog"
输出: true
解释: "a"="dogdog",b="",反之也符合规则

提示:
0 <= len(pattern) <= 1000
0 <= len(value) <= 1000
你可以假设pattern只包含字母"a""b",value仅包含小写字母。

Source: LeetCode
Link: https://leetcode-cn.com/problems/pattern-matching-lcci The
copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

2. Problem solving

Pay attention to examples "ab", "",结果false, easy to go wrong

class Solution {
public:
    bool patternMatching(string pattern, string value) {
        if(pattern==value)
            return true;
        else if(pattern=="" && value!="")
            return false;
        int i, a = 0, b = 0, vlen = value.size();
        for(i = 0; i < pattern.size(); ++i)
        {
            if(pattern[i]=='a')
                a++;
            else
                b++;
        }
        if(a==0 || b==0)
        {
            if(value=="")//只有一种字符,其代表空串即可满足
                return true;
            a = max(a, b);
            return onlyAorB(value, a);
        }
        //a,b均有的情况
        //a,b均可以表示空字符串, "ab",""
        if(onlyAorB(value,a) && value.size()!=0)//b表示空串
            return true;
        if(onlyAorB(value,b) && value.size()!=0)//a表示空串
            return true;
        //a,b均不表示空
        int la=1, lb=1;//a,b代表的长度
        while(la*a < vlen)
        {
            if((vlen-la*a)%b)//不能整除
            {
                la++;
                continue;
            }
            lb = (vlen-la*a)/b;
            if(good(la,lb,pattern,value))
                return true;
            la++;
        }
        return false;
    }
    bool onlyAorB(string& val, int a)
    {
        if(val.size()%a)//不能整除,不行
            return false;
        int n = val.size()/a;
        string sub = val.substr(0,n);
        for(int j = n; j < val.size(); j+=n)
        {
            if(val.substr(j,n) != sub)
                return false;
        }
        return true;
    }
    bool good(int la, int lb, string& pat, string& val)
    {
        int idxa = -1, idxb = -1, i = 0, idx = 0;
        while(idxa==-1 || idxb==-1)
        {	//找到a,b代表的字符的idx
            if(pat[i]=='a')
            {
                if(idxa == -1)
                    idxa = idx;
                idx += la;
            }
            else
            {
                if(idxb == -1)
                    idxb = idx;
                idx += lb;
            }
            i++;
        }
        string sa = val.substr(idxa, la);//a代表的字符
        string sb = val.substr(idxb, lb);//b代表的字符
        int j = 0, delta;
        for(i = 0; i < pat.size(); ++i, j+=delta)
        {
            if(pat[i]=='a')
            {
                delta = la;
                if(val.substr(j,la) != sa)
                    return false;
            }
            else
            {
                delta = lb;
                if(val.substr(j,lb) != sb)
                    return false;
            }
        }
        return true;
    }
};

Insert picture description here

Published 860 original articles · praised 2412 · 450,000 views

Guess you like

Origin blog.csdn.net/qq_21201267/article/details/105571417