字符串中所有的字符是不是在长字符串中出现


有两个字符串,一个常字符串,一个短字符串,以最快的时间判断短字符串中所有的字符是不是在长字符串中出现

    #include <iostream>  
    #include <string>  
      
    using namespace std;  
      
    bool CheckStr(string strShort, string strLong);  
      
    void main()  
    {  
        string strShort = "AbBBcC";   //短字符串   
        string strLong = "aAbBBcCdd"; //长字符串  
      
        if(CheckStr(strShort, strLong))  
        {  
            cout << "TRUE";  
        }  
        else  
        {  
            cout << "FALSE";  
        }  
      
        system("pause");  
    }  
      
    bool CheckStr(string strShort, string strLong)  
    {  
        /* 申请一个52大小的数组用于存储每个字符出现的次数,主要考虑到同时存在大小写 */  
        int hash[52] = {0};    
        int index = 0;  
          
        /* 将长字符串出现的次数统计到数组中 */  
        for (int i = 0; i < strLong.length(); i++)  
        {  
            /* 如果是大写字母 */  
            if ((strLong[i] >= 'A') && (strLong[i] <= 'Z'))  
            {  
                hash[strLong[i] - 'A']++;  
            }  
            else if ((strLong[i] >= 'a') && (strLong[i] <= 'z'))  
            {  
                /* 小写字母从下标26开始存储 */  
                hash[strLong[i] - 'a' + 26]++;  
            }  
            else  
            {  
                cout << "string error";  
                return false;  
            }  
        }  
      
        /* 通过上面的步骤已经将长字符串排序完,下面将轮训短字符串 */  
        for (int i = 0; i < strShort.length(); i++)  
        {  
            /* 如果是大写字母 */  
            if ((strShort[i] >= 'A') && (strShort[i] <= 'Z'))  
            {  
                index = strShort[i] - 'A';  
            }  
            else if ((strShort[i] >= 'a') && (strShort[i] <= 'z'))  
            {  
                /* 小写字母从下标26开始存储 */  
                index = strShort[i] - 'a' + 26;  
            }  
              
            /* 如果hash中对应下标的内容为零,则证明长字符串中没有出现过该字符,直接return false */  
            if (0 == hash[index])  
            {  
                return false;  
            }  
        }  
      
        /* 走到这一步都还没有return false,说明所有的字符都出现过,直接return true */  
        return true;  
    }  







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值