【模拟】Leetcode 数青蛙

题目讲解

1419. 数青蛙


算法讲解

cpp 复制代码
class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        string target = "croak";
        int n = target.size();

        //保存target每个字符的位置index
        unordered_map<char, int>index;
        for(int i = 0; i < n; i++)index[target[i]] = i;

        //按照target的顺序制作Hash
        vector<int>Hash(n); 

        for(int i = 0; i < croakOfFrogs.size(); i++)
        {
            if(croakOfFrogs[i] == 'c')
            {
                if(Hash[n-1] != 0)Hash[n-1]--;
                Hash[0]++;
            }
            else
            {
                //获取当前 croak 字母的下标
                int char_index = index[croakOfFrogs[i]];
                //判断前一个字符是否在Hash中出现 
                if(Hash[char_index - 1] == 0)return -1;
                Hash[char_index - 1]--; Hash[char_index]++;
            }

           
        }
         //怕出现这种croakcro
        for(int i = 0; i < n-1; i++)
        {
            if(Hash[i] != 0)return -1;
        }
        return Hash[n-1];
    }
};
相关推荐
wsxqaz2 小时前
浏览器原生控件上传PDF导致hash值不同
算法·pdf·哈希算法
hn小菜鸡10 小时前
LeetCode 377.组合总和IV
数据结构·算法·leetcode
亮亮爱刷题10 天前
飞往大厂梦之算法提升-7
数据结构·算法·leetcode·动态规划
双叶83610 天前
(C语言)Map数组的实现(数据结构)(链表)(指针)
c语言·数据结构·c++·算法·链表·哈希算法
安全系统学习10 天前
【网络安全】DNS 域原理、危害及防御
算法·安全·web安全·网络安全·哈希算法
zmuy10 天前
124. 二叉树中的最大路径和
数据结构·算法·leetcode
chao_78910 天前
滑动窗口题解——找到字符串中所有字母异位词【LeetCode】
数据结构·算法·leetcode
Alfred king10 天前
面试150跳跃游戏
python·leetcode·游戏·贪心算法
呆呆的小鳄鱼10 天前
leetcode:746. 使用最小花费爬楼梯
算法·leetcode·职场和发展
YuTaoShao10 天前
【LeetCode 热题 100】42. 接雨水——(解法一)前后缀分解
java·算法·leetcode·职场和发展