又是奇瑞,“统一下班时间”过去不久,最近又整新活了...

202 阅读3分钟

奇瑞 345

345 可不是奇瑞的汽车型号,而是奇瑞 7 月份会议文章中提出的新策略。

简单来说,要提高加班效率,实现 3 个人干 5 个人活,拿 4 个人的工资,要把员工当成家人一样看待,要对他们的健康幸福负责。

前面是真心话,后面是心虚话。

3 个人干 5 个人活,拿 4 个人的工资。

真的,论算计,咱们比不过这些企业。

先不说拿 4 个人工资这事能不能落实吧,就算严格按照规则来计算,那也是工作量提高 60%,报酬提高 30%。

赢,双赢,奇瑞赢两次的那种赢。

真的把员工当家人,怎么不提出 4 个人干 3 个人活,拿 5 个人的工资?

奇瑞前不久才被曝光为争取 IPO,取消所有员工的加班时长,统一显示下班时间为 17:00,引起公愤,随着事件发酵,不少奇瑞人指出,平时加班加到只有早上才能见到太阳。

现在还要整新活,提高工作强度,这是有恃无恐到了什么地步?

...

回归主题。

周末了,来一道简单算法题。

题目描述

平台:LeetCode

题号:1684

给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words

如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是一致字符串。

请你返回 words 数组中一致字符串的数目。

示例 1:

输入:allowed = "ab", words = ["ad","bd","aaab","baa","badab"]

输出:2

解释:字符串 "aaab""baa" 都是一致字符串,因为它们只包含字符 'a''b'

示例 2:

输入:allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]

输出:7

解释:所有字符串都是一致的。

示例 3:

输入:allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]

输出:4

解释:字符串 "cc""acd""ac""d" 是一致字符串。

提示:

  • 1<=words.length<=1041 <= words.length <= 10^4
  • 1<=allowed.length<=261 <= allowed.length <= 26
  • 1<=words[i].length<=101 <= words[i].length <= 10
  • allowed 中的字符 互不相同 。
  • words[i] 和 allowed 只包含小写英文字母。

模拟

根据题意模拟即可:为了快速判断某个字符是否在 allowed 出现过,我们可以使用 Set 结构、定长数组或是一个 int 变量(搭配位运算)来对 allowed 中出现的字符进行转存。

随后遍历所有的 words[i]words[i],统计符合要求的字符串个数。

Java 代码:

class Solution {
    public int countConsistentStrings(String allowed, String[] words) {
        boolean[] hash = new boolean[26];
        for (char c : allowed.toCharArray()) hash[c - 'a'] = true;
        int ans = 0;
        out:for (String s : words) {
            for (char c : s.toCharArray()) {
                if (!hash[c - 'a']) continue out;
            }
            ans++;
        }
        return ans;
    }
}

Java 代码:

class Solution {
    public int countConsistentStrings(String allowed, String[] words) {
        int hash = 0, ans = 0;
        for (char c : allowed.toCharArray()) hash |= (1 << (c - 'a'));
        out:for (String s : words) {
            for (char c : s.toCharArray()) {
                if (((hash >> (c - 'a')) & 1) == 0) continue out;
            }
            ans++;
        }
        return ans;
    }
}

TypeScript 代码:

function countConsistentStrings(allowed: string, words: string[]): number {
    const sset = new Set<string>()
    for (const c of allowed) sset.add(c)
    let ans = 0
    out:for (const s of words) {
        for (const c of s) {
            if (!sset.has(c)) continue out
        }
        ans++
    }
    return ans
}

TypeScript 代码:

function countConsistentStrings(allowed: string, words: string[]): number {
    let hash = 0, ans = 0
    for (const c of allowed) hash |= (1 << (c.charCodeAt(0) - 'a'.charCodeAt(0)))
    out:for (const s of words) {
        for (const c of s) {
            if (((hash >> (c.charCodeAt(0) - 'a'.charCodeAt(0))) & 1) == 0) continue out
        }
        ans++
    }
    return ans
}

Python 代码:

class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        sset = set([c for c in allowed])
        ans = 0
        for s in words:
            ok = True
            for c in s:
                if c not in sset:
                    ok = False
                    break
            ans += 1 if ok else 0
        return ans

Python 代码:

class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        num, ans = 0, 0
        for c in allowed:
            num |= (1 << (ord(c) - ord('a')))
        for s in words:
            ok = True
            for c in s:
                if not (num >> (ord(c) - ord('a')) & 1):
                    ok = False
                    break
            ans += 1 if ok else 0
        return ans
  • 时间复杂度:O(m+i=0n1words[i].length)O(m + \sum_{i = 0}^{n - 1}words[i].length),其中 mmallowed 长度,nnwords 长度
  • 空间复杂度:O(m)O(m)