<\/span><\/h2>\n\n\n\nIn this challenge, we use regular expressions (RegEx) to remove instances of words that are repeated more than once, but retain the first occurrence of any case-insensitive repeated word. For example, the words love<\/code> and to<\/code> are repeated in the sentence I love Love to To tO code<\/code>. Can you complete the code in the editor so it will turn I love Love to To tO code into I love to code?<\/code><\/p>\n\n\n\nTo solve this challenge, complete the following three lines:<\/p>\n\n\n\n
- Write a RegEx that will match any repeated word.<\/li>
- Complete the second compile argument so that the compiled RegEx is case-insensitive.<\/li>
- Write the two necessary arguments for replaceAll such that each repeated word is replaced with the very first instance the word found in the sentence. It must be the exact first occurrence of the word, as the expected output is case-sensitive.<\/li><\/ol>\n\n\n\n
Note:<\/strong> This challenge uses a custom checker; you will fail the challenge if you modify anything other than the three locations that the comments direct you to complete. To restore the editor’s original stub code, create a new buffer by clicking on the branch icon in the top left of the editor.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe following input is handled for you the given stub code:<\/p>\n\n\n\n
The first line contains an integer, n<\/em><\/strong>, denoting the number of sentences.
Each of the n <\/em><\/strong>subsequent lines contains a single sentence consisting of English alphabetic letters and whitespace characters.<\/p>\n\n\n\n<\/span>Constraint<\/strong><\/span><\/h4>\n\n\n\n- Each sentence consists of at most 104 <\/sup><\/strong>English alphabetic letters and whitespaces.<\/li>
- 1 <= n <= 100<\/strong><\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nStub code in the editor prints the sentence modified by the replaceAll line to stdout. The modified string must be a modified version of the initial sentence where all repeat occurrences of each word are removed.<\/p>\n\n\n\n
<\/span>Sample Input<\/strong><\/span><\/h4>\n\n\n\n 5\n Goodbye bye bye world world world\n Sam went went to to to his business\n Reya is is the the best player in eye eye game\n in inthe\n Hello hello Ab aB<\/code><\/pre>\n\n\n\n<\/span>Sample Output<\/strong><\/span><\/h4>\n\n\n\n Goodbye bye world\n Sam went to his business\n Reya is the best player in eye game\n in inthe\n Hello Ab<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h4>\n\n\n\n- We remove the second occurrence of
bye<\/code> and the second and third occurrences of world<\/code> from Goodbye bye bye world world world to get Goodbye bye world.<\/code><\/li>- We remove the second occurrence of
went<\/code> and the second and third occurrences of to <\/code>from Sam went went to to to his business to get Sam went to his business.<\/code><\/li>- We remove the second occurrence of
is<\/code>, the second occurrence of the<\/code>, and the second occurrence of eye<\/code> from Reya is is the the best player in eye eye game to get Reya is the best player in eye game.<\/code><\/li>- The sentence
in inthe <\/code>has no repeated words, so we do not modify it.<\/li>- We remove the second occurrence of
ab<\/code> from Hello hello Ab aB<\/code> to get Hello Ab<\/code>. It’s important to note that our matching is case-insensitive, and we specifically retained the first occurrence of the matched word in our final string.<\/li><\/ol>\n\n\n\n<\/span>Solution –<\/strong> Java Regex 2 – Duplicate Words<\/strong><\/span><\/h2>\n\n\n\n import java.util.Scanner;\n\nimport java.util.regex.Matcher;\n\nimport java.util.regex.Pattern;\n\n\npublic class DuplicateWords {\n\n public static void main(String[] args) {\n String regex = \"\\\\b(\\\\w+)(?:\\\\W+\\\\1\\\\b)+\";\n Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);\n Scanner in = new Scanner(System.in);\n int numSentences = Integer.parseInt(in.nextLine());\n \n while (numSentences-- > 0) {\n String input = in.nextLine();\n \n Matcher m = p.matcher(input);\n \n \/\/ Check for subsequences of input that match the compiled pattern\n while (m.find()) {\n input = input.replaceAll(m.group(), m.group(1));\n }\n \n \/\/ Prints the modified sentence.\n System.out.println(input);\n }\n \n in.close();\n }\n} <\/pre>\n\n\n\n