java 检查字符串是否包含字母表中的所有字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3734511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:12:40  来源:igfitidea点击:

Check if string has all the letters of the alphabet

javaalgorithmlogic

提问by Vivek

What would be the best logic to check all the letters in a given string.

检查给定字符串中的所有字母的最佳逻辑是什么。

If all the 26 letters are available in the provided string, I want to check that and perform so ops. eg. Pack my box with five dozen liquor jugs.

如果提供的字符串中所有 26 个字母都可用,我想检查一下并执行操作。例如。用五打酒壶装好我的盒子。

  1. Would using a Hash be useful?
  2. Or using a bit map? or any other way?
  1. 使用哈希会有用吗?
  2. 还是使用位图?或任何其他方式?

BTW my code would be in Java.

顺便说一句,我的代码将使用 Java。

回答by st0le

Using a BitMap, I'm assuming you meant case insenstive.

使用位图,我假设您的意思是不区分大小写。

Update: Solution by Thomas is more efficient, than the following. :) Use that one.

更新:Thomas 的解决方案比以下解决方案更有效。:) 使用那个。

    //
    String test  = "abcdefeghjiklmnopqrstuvwxyz";

    BitSet alpha = new BitSet(26);
    for(char ch : test.toUpperCase().toCharArray())
        if(Character.isLetter(ch))
            alpha.set(ch - 65);

    System.out.println(alpha.cardinality() == 26);

回答by Thomas Mueller

Not yet fully optimized:

尚未完全优化:

public static void main(String... a) {
    String s = "Pack my box with five dozen liquor jugs.";
    int i=0;
    for(char c : s.toCharArray()) {
        int x = Character.toUpperCase(c);
        if (x >= 'A' && x <= 'Z') {
            i |= 1 << (x - 'A');
        }
    }
    if (i == (i | ((1 << (1 + 'Z' - 'A')) - 1))) {
        System.out.println("ok");
    }
}

回答by Vivek

I'd go for a bitmap. If you increment a counter each time you set an entry in the bitmap to 1, you can early return as soon as you've seen all the letters. I hope this is not for enforcing password requirements.

我会去找位图。如果每次将位图中的条目设置为 1 时都增加一个计数器,则可以在看到所有字母后立即返回。我希望这不是为了强制执行密码要求。

回答by MAK

Keep an booleanarray of size 26. Each position of the array says whether a particular character is present or not (a is at 0, b is at 1, etc.). Initially all are set to false. Now do a single scan through the string character by character, setting the value for that character to true. At the end, check if all 26 indexes contain true.

保留一个boolean大小为 26的数组。数组的每个位置表示是否存在特定字符(a 位于 0,b 位于 1,等等)。最初全部都设置为false。现在逐个字符地对字符串进行一次扫描,将该字符的值设置为 true。最后,检查所有 26 个索引是否都为 true。

回答by Guillaume Lebourgeois

An array of 26 booleans is enough, each entry representing on of the alphabet letters. You can set the entry to true when the letter is found.

一个包含 26 个布尔值的数组就足够了,每个条目代表一个字母。找到字母后,您可以将条目设置为 true。

回答by Didier Trosset

I'd go for a sieve algorithm on the 26 letters. Just my $.02.

我会在 26 个字母上使用筛选算法。只是我的 $.02。

Edit: An array of 26 values that represent the 26 letters of the alphabet. Then scan the string, checking each letter as you encounter it. At the end, check if the 26 letters have been checked.

编辑:代表字母表中 26 个字母的 26 个值的数组。然后扫描字符串,在遇到它时检查每个字母。最后,检查是否检查了26个字母。

回答by ANOOP KUMAR

Try this one out it's easy and simple to understand

试试这个,简单易懂

import java.util.*;

public class Pnagram{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String Str=sc.nextLine();
        Set<Character> set = new HashSet<Character>();

        for(char c:Str.toUpperCase().toCharArray()){
            if(Character.isLetter(c))
            set.add(c);
        }
        if(set.size()==26)
            System.out.println("pnagram");
        else
            System.out.println("not pnagram");
    }
}

回答by Narendra Reddy Lingam

    public class Pangram {
    public static boolean isPangram(String test){
        for (char a = 'A'; a <= 'Z'; a++)
            if ((test.indexOf(a) < 0) && (test.indexOf((char)(a + 32)) < 0))
                return false;
        return true;
    }

    public static void main(String[] args){
        System.out.println(isPangram("the quick brown fox jumps over the lazy dog"));//true
        System.out.println(isPangram("the quick brown fox jumped over the lazy dog"));//false, no s
        System.out.println(isPangram("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));//true
        System.out.println(isPangram("ABCDEFGHIJKLMNOPQSTUVWXYZ"));//false, no r
        System.out.println(isPangram("ABCDEFGHIJKL.NOPQRSTUVWXYZ"));//false, no m
        System.out.println(isPangram("ABC.D.E.FGHI*J/KL-M+NO*PQ R\nSTUVWXYZ"));//true
        System.out.println(isPangram(""));//false
        System.out.println(isPangram("Pack my box with five dozen liquor jugs."));//true
    }
}

回答by Raghavendran Gopalakrishnan

This is not an optimal solution, yet easy to understand :) !

这不是最佳解决方案,但易于理解:)!

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String s = in.nextLine().toUpperCase();
    char[] sarray = s.toCharArray();
    char[] alphabets = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    Set<Character> set = new HashSet<Character>();
    for(int i=0;i<sarray.length;i++){
        for(int j=0;j<alphabets.length;j++){
            if(sarray[i] == alphabets[j]){
                set.add(sarray[i]);
                break;
            }
            continue;
        }
    }
    if(set.size() == 26){
        System.out.println("pangram");
    }else{
        System.out.println("not pangram");
    }

}

}

}

回答by AmrDeveloper

Explanation

解释

First I will convert all string chars to uppercase.

首先,我将所有字符串字符转换为大写。

in ASCII Table you can find uppercase chars ranges, i.e. between 65 to 90

在 ASCII 表中,您可以找到大写字符范围,即 65 到 90

A == 65 and Z == 90

A == 65 和 Z == 90

So, forloop should start from 65 and end on 90.

所以,for循环应该从 65 开始,到 90 结束。

Check if the string contains current ASCII char.

检查字符串是否包含当前的 ASCII 字符。

I am converting integer to char and put "" to be string

我正在将整数转换为字符并将“”作为字符串

if !text.contains((char)i + "") == true

This implies that if the string does not have the current char to i, I will return false.

这意味着如果字符串没有当前的字符 to i,我会return false

You can make it even better by using char iin loop

你可以通过使用char iin loop让它变得更好

static boolean isPangram(String text) {
    //change all chars to upper case
    text = text.toUpperCase();
    //Loop from A to Z
    for(int i = 65 ; i < 91 ; i++){
        //if text not contains return false
        if(!text.contains((char)i + "")){   
            return false;
        }
    }
    return true;
}