Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions Java/Strings/JavaRegex2.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,34 @@
in inthe
*/


import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaRegex2
{
public static void main(String[] args){
public class DuplicateWords {

public static void main(String[] args) {

String pattern = "(?i)\\b(\\w+)(\\s+\\1)+\\b";
Pattern r = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE);
String regex = "\\b(\\w+)(\\s\\1)+\\b";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());
while(testCases>0){
int numSentences = Integer.parseInt(in.nextLine());

while (numSentences-- > 0) {
String input = in.nextLine();
Matcher m = r.matcher(input);
boolean findMatch = true;
while(m.find( )){
input = input.replaceAll(pattern,"$1");
findMatch = false;

Matcher m = p.matcher(input);

// Check for subsequences of input that match the compiled pattern
while (m.find()) {
input = input.replaceAll(m.group(), m.group(1));
}

// Prints the modified sentence.
System.out.println(input);
testCases--;
}

in.close();
}
}