📜  检查给定的字符串是否是Java的Pangram

📅  最后修改于: 2021-09-03 03:31:47             🧑  作者: Mango

给定字符串str ,任务是编写Java程序,检查给定的字符串是否是全字符串。

例子:

方法 1 – 使用频率数组:

  1. 将字符串的每个字母转换为大写或大写。
  2. 创建一个频率数组来标记从az的每个字母的频率。
  3. 然后,遍历频率数组,如果给定字符串不存在任何字母,则打印No ,否则打印Yes

下面是上述方法的实现:

Java
// Java program for the above approach
  
class GFG {
  
    static int size = 26;
  
    // Function to check if ch is a letter
    static boolean isLetter(char ch)
    {
        if (!Character.isLetter(ch))
            return false;
  
        return true;
    }
  
    // Function to check if a string
    // contains all the letters from
    // a to z
    static boolean allLetter(String str,
                             int len)
    {
        // Convert the given string
        // into lowercase
        str = str.toLowerCase();
  
        // Create a frequency array to
        // mark the present letters
        boolean[] present = new boolean[size];
  
        // Traverse for each character
        // of the string
        for (int i = 0; i < len; i++) {
  
            // If the current character
            // is a letter
            if (isLetter(str.charAt(i))) {
  
                // Mark current letter as present
                int letter = str.charAt(i) - 'a';
                present[letter] = true;
            }
        }
  
        // Traverse for every letter
        // from a to z
        for (int i = 0; i < size; i++) {
  
            // If the current character
            // is not present in string
            // then return false,
            // otherwise return true
            if (!present[i])
                return false;
        }
        return true;
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given string str
        String str = "Abcdefghijklmnopqrstuvwxyz";
        int len = str.length();
  
        // Function Call
        if (allLetter(str, len))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


Java
// Java program for the above approach
  
class GFG {
  
    // Function to check if a string
    // contains all the letters from
    // a to z (ignoring case)
    public static void
    allLetter(String str)
    {
        // Converting the given string
        // into lowercase
        str = str.toLowerCase();
  
        boolean allLetterPresent = true;
  
        // Loop over each character itself
        for (char ch = 'a'; ch <= 'z'; ch++) {
  
            // Check if the string does not
            // contains all the letters
            if (!str.contains(String.valueOf(ch))) {
                allLetterPresent = false;
                break;
            }
        }
  
        // Check if all letter present then
        // print "Yes", else print "No"
        if (allLetterPresent)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
  
    // Driver Code
    public static void main(String args[])
    {
        // Given string str
        String str = "Abcdefghijklmnopqrstuvwxyz12";
  
        // Function call
        allLetter(str);
    }
}


输出:
Yes


时间复杂度: O(N)
辅助空间: O(26)

方法 2 – 使用遍历:想法是将给定的字符串转换为小写字母,然后遍历从az本身的每个字符,并检查给定的字符串包含从 a 到 z 的所有字母。如果所有字母都存在,则打印Yes ,否则打印No。

下面是上述方法的实现:

Java

// Java program for the above approach
  
class GFG {
  
    // Function to check if a string
    // contains all the letters from
    // a to z (ignoring case)
    public static void
    allLetter(String str)
    {
        // Converting the given string
        // into lowercase
        str = str.toLowerCase();
  
        boolean allLetterPresent = true;
  
        // Loop over each character itself
        for (char ch = 'a'; ch <= 'z'; ch++) {
  
            // Check if the string does not
            // contains all the letters
            if (!str.contains(String.valueOf(ch))) {
                allLetterPresent = false;
                break;
            }
        }
  
        // Check if all letter present then
        // print "Yes", else print "No"
        if (allLetterPresent)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
  
    // Driver Code
    public static void main(String args[])
    {
        // Given string str
        String str = "Abcdefghijklmnopqrstuvwxyz12";
  
        // Function call
        allLetter(str);
    }
}
输出:
Yes


时间复杂度: O(26*N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live