首页 > 其他分享 >【LeeCode】1832. 判断句子是否为全字母句

【LeeCode】1832. 判断句子是否为全字母句

时间:2022-12-15 11:01:47浏览次数:67  
标签:1832 String sentence Solution checkIfPangram 句子 LeeCode new public


【题目描述】

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

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

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

​https://leetcode.cn/problems/check-if-the-sentence-is-pangram/​

【示例】

【LeeCode】1832. 判断句子是否为全字母句_i++

【代码】admin

import java.util.*;

/**
* 2022-12-15
*/

class Solution {
public boolean checkIfPangram(String sentence) {
boolean flag = true;
for (int i = 97; i < 97 + 26; i++){
char c = (char)i;
if (!sentence.contains(c+"")){
flag = false;
}
}
System.out.println(flag);
return flag;
}
}

public class Main {
public static void main(String[] args) {
String str = "thequickbrownfoxjumpsoverthelazydog"; // 输出:true
String str1 = "leetcode"; // 输出:false
new Solution().checkIfPangram(str);
new Solution().checkIfPangram(str1);
}
}


【代码2】可爱抱抱呀

字符串长度小于26,可直接返回false。

import java.util.*;
class Solution {
// 很巧妙的, 基于set来判断是否包含了全部的字符
public boolean checkIfPangram(String sentence) {
Set<Character> set=new HashSet<>();
for(char c:sentence.toCharArray()){set.add(c);}
return set.size()==26;
}
}

public class Main {
public static void main(String[] args) {
String str = "thequickbrownfoxjumpsoverthelazydog"; // 输出:true
String str1 = "leetcode"; // 输出:false
new Solution().checkIfPangram(str);
new Solution().checkIfPangram(str1);
}
}

【代码3】官方

import java.util.*;
class Solution {
public boolean checkIfPangram(String sentence) {
boolean[] exist = new boolean[26];
for (int i = 0; i < sentence.length(); i++) {
char c = sentence.charAt(i);
exist[c - 'a'] = true;
}
for (boolean x : exist) {
if (!x) {
return false;
}
}
return true;
}
}

public class Main {
public static void main(String[] args) {
String str = "thequickbrownfoxjumpsoverthelazydog"; // 输出:true
String str1 = "leetcode"; // 输出:false
new Solution().checkIfPangram(str);
new Solution().checkIfPangram(str1);
}
}

标签:1832,String,sentence,Solution,checkIfPangram,句子,LeeCode,new,public
From: https://blog.51cto.com/u_13682316/5938887

相关文章

  • 【LeeCode】392. 判断子序列
    【题目描述】给定字符串 s 和 t ,判断 s 是否为 t 的子序列。字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例......
  • 力扣每日一题2022.12.12---1832. 判断句子是否为全字母句
    全字母句指包含英语字母表中每个字母至少一次的句子。给你一个仅由小写英文字母组成的字符串sentence,请你判断 sentence是否为全字母句。如果是,返回true;否则,返回......
  • 1832. 判断句子是否为全字母句
    1832.判断句子是否为全字母句classSolution{publicbooleancheckIfPangram(Stringsentence){char[]chars=sentence.toCharArray();int[]......
  • 【LeeCode】剑指 Offer 42. 连续子数组的最大和
    【题目描述】输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为O(n)​​https://leetcode.cn/problems/lian-xu-zi-......
  • 【LeeCode】42. 接雨水
    【题目描述】给定 ​​n​​ 个非负整数表示每个宽度为 ​​1​​ 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。​​https://leetcode.cn/problems/trappin......
  • 【LeeCode】1748. 唯一元素的和
    【题目描述】给你一个整数数组nums。数组中唯一元素是那些只出现恰好一次的元素。请你返回nums中唯一元素的和 ​​https://leetcode.cn/problems/sum-of-unique-el......
  • leecode
    算法:数据结构+操作 数据结构:基本结构数组+链表其他结构 操作:增删改查、聪明的穷举 数组算法:快慢指针、左右指针链表算法:双指针 二叉树算法:  ......
  • 句子成分&分类 被动
    句子成分&分类简单句的两个主要句子成分,分别是主语+谓语谓语的核心是谓语动词谓语不等于谓语动词主语谓语[Therabbit][ateacarrot]ate为谓......
  • 【LeeCode】94. 二叉树的中序遍历
    【题目描述】给定一个二叉树的根节点 ​​root​​ ,返回 它的 中序 遍历 。​​​https://leetcode.cn/problems/binary-tree-inorder-traversal/description/?favori......
  • 【LeeCode】104. 二叉树的最大深度
    【题目描述】给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。​​https://leetcode.cn/p......