201.md 2.8 KB
Newer Older
W
wizardforcel 已提交
1
# 题目 – 检查字符串是否完整(包含所有字母)
W
wizardforcel 已提交
2 3 4

> 原文: [https://howtodoinjava.com/puzzles/puzzle-check-if-string-is-complete-contains-all-alphabets/](https://howtodoinjava.com/puzzles/puzzle-check-if-string-is-complete-contains-all-alphabets/)

W
wizardforcel 已提交
5
如果字符串包含从 a 到 z 的所有字符,则认为该字符串是完整的。 给定一个字符串,检查它是否完整。 例如:
W
wizardforcel 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

**样本输入**

```java
3
wyyga
qwertyuioplkjhgfdsazxcvbnm
ejuxggfsts
```

**样本输出**

```java
NO
YES
NO
```

W
wizardforcel 已提交
24
## 使用`for`循环和`indexOf()`的解决方案
W
wizardforcel 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

我写了一个快速的功能,可以找到这个完整的字符串。

```java
package com.howtodoinjava.examples;

public class CheckAllAlphabetsAlgorithms
{
    public static void main(String[] args)
    {
        System.out.println( checkAllChars( "qwertyuioplkjhgfdsAzxcvbnm" ) );
        System.out.println( checkAllChars( "123" ) );
        System.out.println( checkAllChars( "ejuxggfsts" ) );
        System.out.println( checkAllChars( "wyyga" ) );
    }

    private static String checkAllChars ( String input )
    {
        //If input length is less than 26 then it can never be complete
        if(input.length() < 26)
        {
            return "FALSE";
        }

        for (char ch = 'A'; ch <= 'Z'; ch++)
        {
            if (input.indexOf(ch) < 0 && input.indexOf((char) (ch + 32)) < 0)
            {
                return "FALSE";
            }
        }
        return "TRUE";
    }
}
```

W
wizardforcel 已提交
61
**输出**
W
wizardforcel 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

```java
TRUE
FALSE
FALSE
FALSE
```

## 用于正则表达式的解决方案

这是一个使用正则表达式查找完整字符串的解决方案(很丑,因为我不喜欢长的正则表达式)。

```java
package com.howtodoinjava.examples;

public class CheckAllAlphabetsAlgorithms
{
    public static void main(String[] args)
    {
        System.out.println( checkAllCharsUsingRegex( "qwertyuioplkjhgfdsAzxcvbnm" ) );
        System.out.println( checkAllCharsUsingRegex( "123" ) );
        System.out.println( checkAllCharsUsingRegex( "ejuxggfsts" ) );
        System.out.println( checkAllCharsUsingRegex( "wyyga" ) );
    }

    private static String checkAllCharsUsingRegex ( String input )
    {
        //If input length is less than 26 then it can never be complete
        if(input.length() < 26)
        {
            return "FALSE";
        }

        String regex = "(?i)(?=.*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).*";

        if(input.matches(regex)){
            return "TRUE";
        }
        return "FALSE";
    }
}
```

W
wizardforcel 已提交
108
**输出**
W
wizardforcel 已提交
109 110 111 112 113 114 115 116 117

```java
TRUE
FALSE
FALSE
FALSE
```

**祝您学习愉快!**