A string is said to be complete if it contains all the characters from a to z. Given a string, check if it complete or not.
Input
First line of the input contains the number of strings N. It is followed by N lines each contains a single string.
Output
For each test case print "YES" if the string is complete, else print "NO"Constraints
1 <= N <= 10
The length of the string is at max 100 and the string contains only the characters a to z.
Here are the two solutions that I wrote:
class TestClass {
private static byte N;
public static void main(String args[] ) throws Exception {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
N = Byte.parseByte(keyboard.readLine());
for (byte i = 0; i < N; i++) {
String testingString = keyboard.readLine();
isCompleteString(testingString);
}
}
private static void isCompleteString(String testingString) {
String resultString= "";
char[] toChar = testingString.toCharArray();
int length = toChar.length;
for (int i = 0; i < length; i++) {
if(resultString.indexOf(toChar[i])==-1){
resultString=resultString.concat(toChar[i]+"");
}
}
if(resultString.length()==26)System.out.println("YES");
else System.out.println("NO");
}
}
This took 1.6469 secs against various inputs, while this code took:
class TestClass {
private static byte N;
public static void main(String args[] ) throws Exception {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
N = Byte.parseByte(keyboard.readLine());
for (byte i = 0; i < N; i++) {
String testingString = keyboard.readLine();
isCompleteString(testingString);
}
}
private static void isCompleteString(String testingString) {
char[] toChar = testingString.toCharArray();
int length = toChar.length;
boolean[] findingAlphabet = new boolean[26];
for (int i = 0; i < length; i++) {
byte alphabets = (byte) toChar[i];
findingAlphabet[122-alphabets]=true;
}
int count=0;
for(boolean b:findingAlphabet){
if(b)
count++;
}
if(count==26)System.out.println("YES");
else System.out.println("NO");
}
}
1.5818 secs against various inputs.
How can I optimize these programs further (I have used BitSet
)? What are other possible optimized methods of doing the same?