Why when I use the method contains in a java list don't return true when I match the string?
Im doing an exercise in HackerEarth and I can't pass a test. this is the text of exercise:
Arpasland has surrounded by attackers. A truck enters the city. The driver claims the load is food and medicine from Iranians. Ali is one of the soldiers in Arpasland. He doubts about the truck, maybe it's from the siege. He knows that a tag is valid if the sum of every two consecutive digits of it is even and its letter is not a vowel. Determine if the tag of the truck is valid or not.
We consider the letters "A","E","I","O","U","Y" to be vowels for this problem.
Input Format
The first line contains a string of length 9. The format is "DDXDDD-DD", where D stands for a digit (non zero) and X is an uppercase english letter.
Output Format
Print "valid" (without quotes) if the tag is valid, print "invalid" otherwise (without quotes)
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner s = new Scanner(System.in);
// Write your code here
int sum = 0;
boolean flagIsNotVowel = false;
boolean flagIsEven = false;
String word = s.nextLine();
System.out.println(word);
List<String> dic = new ArrayList();
dic.add("A");
dic.add("E");
dic.add("I");
dic.add("O");
dic.add("U");
dic.add("Y");
System.out.println()
for(int i =0;i<word.length();i ){
boolean flag = Character.isDigit(word.charAt(i));
if(flag) {
sum =Integer.parseInt(word.charAt(i) "");
flagIsEven = sum%2==0 ? true : false;
}
if(!flag && !dic.contains(word.charAt(i) ){
System.out.println(word.charAt(i) "letter");
System.out.println(dic.contains(word.charAt(i)) "true");
sum = 0;
flagIsNotVowel = true;
}
if(!flag && dic.contains(word.charAt(i))){
System.out.println(word.charAt(i) "ciao da dic");
sum = 0;
flagIsNotVowel = false;
}
}
if(flagIsEven ==true && flagIsNotVowel == true){
System.out.println("valid");
}
else{
System.out.println("invalid");
}
}}
this is one test that I can't pass : 13A357-22
CodePudding user response:
The problem with that test is that the letter is a vowel. Your code, when dealing with the A
character, correctly sets flagIsNotVowel
to false
.
However, later you deal with the -
character no different than any other non-digit character. -
is not a vowel, so your code now sets flagIsNotVowel
to true.
You need to do absolutely nothing when the character is a dash; if the dash causes you to set flagIsNotVowel
to anything, your code will not be correct anymore.
Note that continue;
will end processing of the loop and immediately skip to the next iteration. This may be useful.
CodePudding user response:
I assume that tag is to have exactly one capital letter (that is not a vowel) and that all other characters are digits.
The tag is valid if and only if it matches the following regular expression:
^(?=\d*[B-DF-HJ-NP-TV-XZ]\d*$)(?!.*(?:[02468][13579]|[13579][02468])).*
This expression can be broken down as follows.
^ # match the beginning of the string
(?= # start a positive lookahead
\d* # match zero or more digits
[B-DF-HJ-NP-TV-XZ] # match a capital letter that is a consonant
\d* # match zero or more characters
$ # match the end of the string
) # end the positive lookahead
(?! # begin a negative lookahead
.* # match zero or more characters
(?: # begin a non-capture group
[02468][13579] # match an even digit followed by an odd digit
|
[13579][02468] # match an odd digit followed by an even digit
) # end non-capture group
) # end the negative lookahead
.* # match zero or more characters (the entire string)