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.
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(m.group(),m.group(1));}// Prints the modified sentence.System.out.println(input);}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 →