一道字节跳动面试题——判断字符串交错


 

判断字符串交错

给定三个字符串a, b, c, 判断c是否可以通过字符串a, b的子串按顺序交错组成.  例如: a: "btdne" b: "yeac" c: "bytedance" 返回: True a: "bytece" b: "dan" c: "bytedance" 返回: True a: "bytec" b: "dan" c: "bytedance" 返回: False a: "byte" b: "danced" c: "bytedance" 返回: False   输入描述 分别输入三行, 每行均为string类型 输出描述 True/False 示例1

输入

btdne yeac bytedance

输出

True    
#include 
#include

#define MAX_SIZE 0x500

//a: "btdne" b: "yeac" c: "bytedance"
//a: "bytece" b: "dan" c: "bytedance"
//a: "bytec" b: "dan" c: "bytedance"

void main()
{
    char szStra[MAX_SIZE] = { 0 };
    char szStrb[MAX_SIZE] = { 0 };
    char szStrc[MAX_SIZE] = { 0 };

    scanf_s("%s", szStra, MAX_SIZE);
    scanf_s("%s", szStrb, MAX_SIZE);
    scanf_s("%s", szStrc, MAX_SIZE);

    /*scanf(szStra, "%s");
    scanf(szStrb, "%s");
    scanf(szStrc, "%s");*/

    char *pStra = szStra;
    char *pStrb = szStrb;
    char *pStrc = szStrc;

    bool bRet = false;

    while (*pStrc != 0)
    {
        if (*pStra != *pStrc)
        {
            break;
        }

        while (*pStra == *pStrc && *pStra != 0 && *pStrc != 0)
        {
            pStra++;
            pStrc++;
        }

        if (*pStrb != *pStrc)
        {
            break;
        }

        while (*pStrb == *pStrc && *pStra != 0 && *pStrc != 0)
        {
            pStrb++;
            pStrc++;
        }
    }

    if (*pStrc == 0)
    {
        bRet = true;
        printf("True\n");
    }
    else
    {
        printf("False\n");
    }

    system("pause");
}