奇瑞 345
345 可不是奇瑞的汽车型号,而是奇瑞 7 月份会议文章中提出的新策略。
![alt](https://img-blog.csdnimg.cn/img_convert/afc485d1a281832de48c1e1e3c5d446a.png)
简单来说,要提高加班效率,实现 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" 是一致字符串。
提示:
-
-
-
-
allowed
中的字符 互不相同 。 -
words[i]
和allowed
只包含小写英文字母。
模拟
根据题意模拟即可:为了快速判断某个字符是否在 allowed
出现过,我们可以使用 Set
结构、定长数组或是一个 int
变量(搭配位运算)来对 allowed
中出现的字符进行转存。
随后遍历所有的 ,统计符合要求的字符串个数。
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
-
时间复杂度: ,其中 为 allowed
长度, 为words
长度 -
空间复杂度:
最后
巨划算的 LeetCode 会员优惠通道目前仍可用 ~
使用福利优惠通道 leetcode.cn/premium/?promoChannel=acoier,年度会员 有效期额外增加两个月,季度会员 有效期额外增加两周,更有超大额专属 🧧 和实物 🎁 福利每月发放。
我是宫水三叶,每天都会分享算法知识,并和大家聊聊近期的所见所闻。
欢迎关注,明天见。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地 🎉🎉