掘金 后端 ( ) • 2022-08-04 12:55

theme: juejin

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第7天,点击查看活动详情


1832. 判断句子是否为全字母句:

全字母句 指包含英语字母表中每个字母至少一次的句子。

给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句

如果是,返回 true ;否则,返回 false

样例 1:

输入:
sentence = "thequickbrownfoxjumpsoverthelazydog"

输出:
true

解释:
sentence 包含英语字母表中每个字母至少一次。

样例 2:

输入:
sentence = "leetcode"

输出:
false

提示:

  • 1 <= sentence.length <= 1000
  • sentence 由小写英语字母组成

分析

  • 面对这道算法题目,二当家的陷入了沉思。
  • 关键是如何存储遍历到哪些字母,利用集合,列表,map,数组都可以。
  • 小写字母一共26个,所以也可以使用整形变量的位运算进行存储。
  • 比较纠结的一个点是句子的长度介于1到1000,那到底是完全遍历完句子再判断是否全字母比较高效,还是遍历每个字母都去判断一次是否全字母比较高效呢。

题解

java

class Solution {
    public boolean checkIfPangram(String sentence) {
        if (sentence.length() < 26) {
            return false;
        }

        int ans = 0;
        for (char c : sentence.toCharArray()) {
            ans |= 1 << (c - 'a');
            if (ans == 0b11111111111111111111111111) {
                return true;
            }
        }

        return false;
    }
}

c

bool checkIfPangram(char * sentence) {
    int ans = 0;
    while (*sentence) {
        ans |= 1 << (*sentence - 'a');
        if (ans == 0b11111111111111111111111111) {
            return true;
        }
        ++sentence;
    }

    return false;
}

c++

class Solution {
public:
    bool checkIfPangram(string sentence) {
        if (sentence.length() < 26) {
            return false;
        }

        int ans = 0;
        for (char &c: sentence) {
            ans |= 1 << (c - 'a');
            if (ans == 0b11111111111111111111111111) {
                return true;
            }
        }

        return false;
    }
};

python

class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        if len(sentence) < 26:
            return False
        ans = 0
        for c in sentence:
            ans |= 1 << (ord(c) - 97)
            if ans == 0b11111111111111111111111111:
                return True
        return False
        

go

func checkIfPangram(sentence string) bool {
    if len(sentence) < 26 {
        return false
    }
    ans := 0
    for _, c := range sentence {
        ans |= 1 << (c - 'a')
        if ans == 0b11111111111111111111111111 {
            return true
        }
    }
    return false
}

rust

impl Solution {
    pub fn check_if_pangram(sentence: String) -> bool {
        if sentence.len() < 26 {
            return false;
        }
        let mut ans = 0;
        for c in sentence.as_bytes() {
            ans |= 1 << (c - 'a' as u8);
            if ans == 0b11111111111111111111111111 {
                return true;
            }
        }
        false
    }
}

typescript

function checkIfPangram(sentence: string): boolean {
    if (sentence.length < 26) {
        return false;
    }

    let ans = 0;
    for (let i = 0; i < sentence.length; ++i) {
        ans |= 1 << (sentence.charCodeAt(i) - 97);
        if (ans == 0b11111111111111111111111111) {
            return true;
        }
    }

    return false;
};

原题传送门:https://leetcode-cn.com/problems/check-if-the-sentence-is-pangram/


非常感谢你阅读本文~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://juejin.cn/user/2771185768884824/posts 博客原创~