【模拟】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];
    }
};
相关推荐
All for pursuit.3 小时前
【二叉树-10】114.二叉树展开为链表
数据结构·c++·算法·leetcode·链表
黄金龙PLUS5 小时前
5个1024比特大状态置换算法的设计与分析
算法·网络安全·密码学·哈希算法·同态加密
旖旎夜光6 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
another heaven6 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
CoderYanger6 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
圣保罗的大教堂6 天前
leetcode 3524. 求出数组的 X 值 I 中等
leetcode
Tim_106 天前
【LeetCode】338、比特位计数
c++·算法·leetcode
mmmmath_36 天前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
Navigator_Z6 天前
LeetCode //C - 1255. Maximum Score Words Formed by Letters
c语言·算法·leetcode
All for pursuit.6 天前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode