patternjavaMinor
Checking if all the characters from an A-Z is present in the string
Viewed 0 times
thepresentallcheckingcharactersfromstring
Problem
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.
followed by N lines each contains a single string.
print "NO"
Constraints
the string contains only the characters a to z.
Here are the two solutions that I wrote:
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[
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 isfollowed by N lines each contains a single string.
Output For each test case print "YES" if the string is complete, elseprint "NO"
Constraints
1 <= N <= 10 The length of the string is at max 100 andthe 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[
Solution
private static byte N;We know that
N
toChar[i]+"" creates a String from each char needlessly
resultString.concat(...) creates a new String every time
You could use a StringBuilder here.
But creating a String only to measure it's length is plain wrong. You could count the chars instead.
But counting the chars is plain wrong too. If you want to know if a beer crate is full, do you count the bottles? Or do you if there's a bottle missing?
if(resultString.length()==26)System.out.println("YES");
else System.out.println("NO");
This should be written as
System.out.println(resultString.length()==26 ? "YES" : "NO");
apart from that 1. there should be no resultString, 2. there should be a method computing and a method printing.
Second version
boolean[] findingAlphabet = new boolean[26];
No good name. What about foundChars or simply found?
byte alphabets = (byte) toChar[i];
What byte? Never use a byte, unless either it's exactly what's needed or you have a big bunch of them.
findingAlphabet[122-alphabets]=true;
Nice obfuscation. What about
found[chars[i] - 'a'] = true;
? And bottle counting again
int count=0;
for(boolean b:findingAlphabet){
if(b)
count++;
}
This should be
for (boolean b : found) {
if (!b) {
return false;
}
}
return true;
How can I optimaize these programs further (I have used BitSet)?
You haven't. But BitSet is slower than a boolean[] (except when memory locality comes in play).
It looks like most of the time gets spend on IO. Not surprising, even the first solution with doing 26 times indexOf` on a 100 chars string is not computationally intensive.It could be sped up by avoiding strings and working with the input bytes directly. But I'd concentrate on readability first, otherwise it'll become a huge mess.
You may want to specify input encoding as US-ASCII as it's sufficient and much faster than UTF-8 which may be your platform default.
Code Snippets
private static byte N;BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));isCompleteString(testingString);String resultString= "";char[] toChar = testingString.toCharArray();
int length = toChar.length;
for (int i = 0; i < length; i++) {Context
StackExchange Code Review Q#91887, answer score: 4
Revisions (0)
No revisions yet.