-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidatingStringsRegex.java
More file actions
45 lines (36 loc) · 1.34 KB
/
Copy pathValidatingStringsRegex.java
File metadata and controls
45 lines (36 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
Given a string write a regex expression to find all strings that start and end with same letter
example:
"a", "aa", and "bababbb" is a match
"ab" and "baba" is not a match
**/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
final static Scanner scan = new Scanner(System.in);
final static String fileName = System.getenv("OUTPUT_PATH");
final static String regularExpression = "^(a).*(a)$|^(b).*(b)$"; //my code...
public static void main(String[] args) throws IOException
{
int query = Integer.parseInt(scan.nextLine());
String[] result = new String[query];
Arrays.fill(result, "False");
for (int i = 0; i < query; i++)
{
String someString = scan.nextLine();
// if(someString.length()==0) result[i] = "True";
if(someString.length()==1 || someString.length()==0) result[i] = "True"; //my code, dealing with edge case of single string
else if (someString.matches(regularExpression)) {
result[i] = "True";
}
}
BufferedWriter fileOut = new BufferedWriter(new FileWriter(fileName));
for (String res: result) {
fileOut.write(res + "\n");
}
fileOut.close();
}
}