-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathcheck panagram.java
93 lines (72 loc) · 2.08 KB
/
check panagram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
61
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
/*
Check whether a string is a Panagram or not.
Note that a string is a Panagram if it contains all the character of the alphabets ignoring the case of the alphabets
For example, str = “Abcdefghijklmnopqrstuvwxyz”
Output: Yes
Explanation: The given string contains all the letters from a to z (ignoring case).
str = "AbhayChetri"
Output: No
Explaination: The given string doesn't contain all the letters from a to z.
Approach:- 1.Convert each letter of the string to the lower or upper case.
2.Create a frequency array to mark the frequency of each letter from a to z.
3.Then, traverse the frequency array and if there is any letter that is not present in the given string then print "No", otherwise print "Yes".
Time Complexity: O(N)
Auxiliary Space: O(26)
*/
class Abhay {
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");
}
}