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.
With this code I got the same expected results except for a mysterious character at the end of the last test case. It is neither a whitespace or unprintable char. It makes no sense for me.
importjava.util.Scanner;importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassDuplicateWords{publicstaticvoidmain(String[]args){Stringregex="\\b(\\w+)\\s+\\1\\b";Patternp=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);Scannerin=newScanner(System.in);intnumSentences=Integer.parseInt(in.nextLine());while(numSentences-->0){Stringinput=in.nextLine();Matcherm=p.matcher(input);// Check for subsequences of input that match the compiled patternwhile(m.find()){input=input.replaceAll("(?i)"+regex,"$1");//m = p.matcher(input);}// Prints the modified sentence.System.out.print(input+'\n');}in.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 →
With this code I got the same expected results except for a mysterious character at the end of the last test case. It is neither a whitespace or unprintable char. It makes no sense for me.