Why is my code compiled as WRONG , is any error in the code ? EZSPEAK

Compiled as WRONG Answer HELP with the code??

what is my error in the code
PROBLEM : EZSPEAK
LINK : EZSPEAK Summary

/* package codechef; // don't place package name! */

import java.util.*;
import java.util.regex.*;  
import java.lang.*;
import java.io.*;
import java.util.regex.Pattern;  
import java.util.Scanner;  
import java.util.regex.Matcher;    

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
	try{	
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		while(n>=1 || n<=100){
		    
		      int count = sc.nextInt();
		if(count>=1 || count<=100){
		      String word = sc.next().toString().toLowerCase();
		 
		  
		  if(count<=4)
		  {
		      System.out.println("YES");
		  }
		  else
		  {
		      String sub = word.substring(0,4);
		      if(sub.contains("a") ||  sub.contains("e") || sub.contains("i") || sub.contains("o") || sub.contains("u"))
		      {
		          System.out.println("Yes");
		      }
		      else
		      {
		          System.out.println("No");
		      }
		  }
		}
		}
	
	}
	catch(Exception e){
	    return;
	}
	
	}
}

try-catch
try{
...
}catch(Exception e){return;}

this is kinda horrible. If you catch an Exception, you have to handle it. At least print the stacktrace. When getting exceptions, closing your eyes and pretending they don’t exist is not the solution!
I removed this since it is not helping.

while true
while(n>=1 || n<=100)

is the same as

while(true)

this is not what you want. You want to decrease n somewhere. Also, this should be t, not n

while(t-->0)

is what you want

unnecessary if statement

In the following I replace “count” with “n” to make it less confusing for myself.

if(n>=1 || n<=100){
...
}

that if statement is not needed since the input already constrains n to be between (inclusive) 1 and 100. So I remove it.

slightly wrong edge case
if(n<=4){
	System.out.println("YES");
}

this should be n<4. Remember that if we have a word with exactly 4 consonants, we need to print “NO”

missing loop
else {
      String sub = word.substring(0,4);
      if(sub.contains("a") ||  sub.contains("e") || sub.contains("i") || sub.contains("o") || sub.contains("u")){
            System.out.println("YES");
      }else{
            System.out.println("NO");
      }
}

Here you are only ever checking for the first 4 chars. But you need to check for all 4 consecutive characters.
So a loop is needed.

Here is a fixed version: CodeChef: Practical coding for everyone

2 Likes

Thank you brother!!

1 Like