You would like to set a password for a bank account. However, there are three restrictions on the format of the password:
- it has to contain only alphanumerical characters (a−z, A−Z, 0−9);
- there should be an even number of letters;
- there should be an odd number of digits.
You are given a string S consisting of N characters. String S can be divided into words by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.
For example, given "test 5 a0A pass007 ?xy1", there are five words and three of them are valid passwords: "5", "a0A" and "pass007". Thus the longest password is "pass007" and its length is 7. Note that neither "test" nor "?xy1" is a valid password, because "?" is not an alphanumerical character and "test" contains an even number of digits (zero).
Write a function:
class Solution { public int solution(String S); }
that, given a non-empty string S consisting of N characters, returns the length of the longest word from the string that is a valid password. If there is no such word, your function should return −1.
For example, given S = "test 5 a0A pass007 ?xy1", your function should return 7, as explained above.
Assume that:
- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
String[] arrS = S.split(" ");
String condition = "^[0-9a-zA-Z]*$";
int maxLength = Integer.MIN_VALUE;
for (String s : arrS) {
if (s.length() <= 3) {
continue;
}
if (Pattern.matches(condition, s)) {
int numCnt = 0; // 숫자의 개수
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 0 || s.charAt(i) <= 9) {
numCnt++;
}
}
int alphaCnt = s.length() - numCnt;
if (numCnt % 2 == 1 && alphaCnt % 2 == 0) {
//tSet.add(s);
maxLength = Math.max(maxLength, s.length());
}
}
}
return Math.max(maxLength, -1);
}
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.regex.Pattern;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
String[] arrS = S.split(" ");
String condition = "^[0-9a-zA-Z]*$";
int maxLength = Integer.MIN_VALUE;
for (String s : arrS) {
if (s.length() <= 3) {
continue;
}
if (Pattern.matches(condition, s)) {
int numCnt = 0; // 숫자의 개수
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 0 || s.charAt(i) <= 9) {
numCnt++;
}
}
int alphaCnt = s.length() - numCnt;
if (numCnt % 2 == 1 && alphaCnt % 2 == 0) {
//tSet.add(s);
maxLength = Math.max(maxLength, s.length());
}
}
}
return Math.max(maxLength, -1);
}
}
}
Solution.java:36: error: class, interface, or enum expected } ^ 1 error
// you can also use imports, for example:
// import java.util.*;
import java.util.regex.Pattern;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
String[] arrS = S.split(" ");
String condition = "^[0-9a-zA-Z]*$";
int maxLength = Integer.MIN_VALUE;
for (String s : arrS) {
if (s.length() <= 3) {
continue;
}
if (Pattern.matches(condition, s)) {
int numCnt = 0; // 숫자의 개수
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 0 || s.charAt(i) <= 9) {
numCnt++;
}
}
int alphaCnt = s.length() - numCnt;
if (numCnt % 2 == 1 && alphaCnt % 2 == 0) {
//tSet.add(s);
maxLength = Math.max(maxLength, s.length());
}
}
}
return Math.max(maxLength, -1);
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.regex.Pattern;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
String[] arrS = S.split(" ");
String condition = "^[0-9a-zA-Z]*$";
int maxLength = Integer.MIN_VALUE;
for (String s : arrS) {
if (s.length() <= 3) {
continue;
}
if (Pattern.matches(condition, s)) {
int numCnt = 0; // 숫자의 개수
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 0 || s.charAt(i) <= 9) {
numCnt++;
}
}
int alphaCnt = s.length() - numCnt;
if (numCnt % 2 == 1 && alphaCnt % 2 == 0) {
//tSet.add(s);
maxLength = Math.max(maxLength, s.length());
}
}
}
return Math.max(maxLength, -1);
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.regex.Pattern;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
String[] arrS = S.split(" ");
String condition = "^[0-9a-zA-Z]*$";
int maxLength = Integer.MIN_VALUE;
for (String s : arrS) {
if (s.length() <= 3) {
continue;
}
if (Pattern.matches(condition, s)) {
int numCnt = 0; // 숫자의 개수
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 0 || s.charAt(i) <= 9) {
numCnt++;
}
}
int alphaCnt = s.length() - numCnt;
if (numCnt % 2 == 1 && alphaCnt % 2 == 0) {
//tSet.add(s);
maxLength = Math.max(maxLength, s.length());
}
}
}
return Math.max(maxLength, -1);
}
}
The following issues have been detected: wrong answers.
For example, for the input '0' the solution returned a wrong answer (got -1 expected 1).
all words contain only alphanumerical characters
got 11 expected 5
valid passwords joined with some invalid characters
got -1 expected 3