We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numSentences = scanner.nextInt();
scanner.nextLine();
while (numSentences-- > 0) {
String input = scanner.nextLine();
String regex = "\\b(\\w+)(\\s+\\1\\b)+"; // Write a RegEx that will match any repeated word
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); // Complete the second compile argument so that the compiled RegEx is case-insensitive
Matcher m = p.matcher(input);
// Replace repeated words with an empty string to remove them completely
input = m.replaceAll("");
System.out.println(input);
}
i am able to get the output but still it is not passing the testcase
scanner.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Regex 2 - Duplicate Words
You are viewing a single comment's thread. Return to all comments →
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern;
public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int numSentences = scanner.nextInt(); scanner.nextLine();
}