forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaRegex2-DuplicateWords.java
More file actions
27 lines (23 loc) · 884 Bytes
/
JavaRegex2-DuplicateWords.java
File metadata and controls
27 lines (23 loc) · 884 Bytes
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
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DuplicateWords
{
public static void main(String[] args){
String pattern = "\\b(\\w+)(\\b\\W+\\b\\1\\b)*";
Pattern r = Pattern.compile(pattern, Pattern.MULTILINE+Pattern.CASE_INSENSITIVE);
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());
while(testCases>0){
String input = in.nextLine();
Matcher m = r.matcher(input);
boolean findMatch = true;
while(m.find( )){
input = input.replaceAll(m.group(),m.group(1));
findMatch = false;
}
System.out.println(input);
testCases--;
}
}
}