Java contain string search in codechef problem

Viewed 161

I am new to codechef. Tried solving simple problem at FANCY problem statement. Copying below problem statement for easy reference:

Chef was reading some quotes by great people. Now, he is interested in classifying all the fancy quotes he knows. He thinks that all fancy quotes which contain the word "not" are Real Fancy; quotes that do not contain it are regularly fancy.

You are given some quotes. For each quote, you need to tell Chef if it is Real Fancy or just regularly fancy.

Input The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first and only line of each test case contains a single string S denoting a quote.

Output For each test case, print a single line containing the string "Real Fancy" or "regularly fancy" (without quotes).

Constraints 1≤T≤50 1≤|S|≤100 each character of S is either a lowercase English letter or a space Subtasks Subtask #1 (100 points): original constraints

Example Input

2

i do not have any fancy quotes

when nothing goes right go left

Example Output

Real Fancy

regularly fancy

My solution is below which runs fine locally on my machine but gives NZEC exception when I try to run on codechef. Submitting this solution results in "Wrong Answer". Used try/catch block because I read here that it will avoid the exception but still answer is not right. I don't understand where am I wrong in this simple problem.

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

import java.util.Scanner;

public class Main
{
    private final static String REAL_FANCY   = "Real Fancy\n";
    private final static String REGULARLY_FANCY  = "regularly fancy\n";
    private final static String MATCH_WORD   = " not "; //note: space both sides
    private final static String MATCH_WORD_START = "not "; //note: space at end
    private final static String MATCH_WORD_END   = " not";//note: space at start

    public static void main(String[] args)
    {
    try
    {
        Scanner scanner = new Scanner(System.in);
        int testCases = Integer.parseInt(scanner.nextLine());

        StringBuilder output = new StringBuilder();
        String str = null;
        for (int i = 0; i < testCases; i++)
        {
        str = scanner.nextLine();
        if (str.startsWith(MATCH_WORD_START) || str.contains(MATCH_WORD)
            || str.endsWith(MATCH_WORD_END))
            output.append(REAL_FANCY);
        else
            output.append(REGULARLY_FANCY);
        }
        scanner.close();
        System.out.println(output.substring(0, output.length() - 1));
    }
    catch (Exception e)
    {
    }
    }
}
1 Answers

There is a logic issue with your code as it fails just recognize 'not ' (notice the space) as a Fancy word.

You can change your if logic and use a regex instead.

if (str.matches(".*\\bnot\\b.*")) {
    output.append(REAL_FANCY);
} else {
    output.append(REGULARLY_FANCY);
}

Here the regex pattern matches the whole word not only. In detail explanation of this regex can be found here.

Related