-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaRegexDuplicateWords.java
More file actions
102 lines (85 loc) · 3.83 KB
/
JavaRegexDuplicateWords.java
File metadata and controls
102 lines (85 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Problem:
//
// In 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 and to are repeated in the sentence I love Love to To tO 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?
//
// To solve this challenge, complete the following three lines:
//
// Write a RegEx that will match any repeated word.
// Complete the second compile argument so that the compiled RegEx is case-insensitive.
// 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.
//
//
// Note: 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.
//
// Input Format
//
// The following input is handled for you the given stub code:
//
// The first line contains an integer, n, denoting the number of sentences.
// Each of the n subsequent lines contains a single sentence consisting of
// English alphabetic letters and whitespace characters.
//
// Constraints
//
// Each sentence consists of at most 10^4 English alphabetic letters and whitespaces.
// 1<=n<=100
//
// Output Format
//
// Stub 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.
//
// Sample Input
// 5
// Goodbye bye bye world world world
// Sam went went to to to his business
// Reya is is the the best player in eye eye game
// in inthe
// Hello hello Ab aB
//
// Sample Output
// Goodbye bye world
// Sam went to his business
// Reya is the best player in eye game
// in inthe
// Hello Ab
//
// Explanation
// 1. We remove the second occurrence of bye and the second and third occurrences of "world" from "Goodbye bye bye world world world" to get "Goodbye bye world".
// 2. We remove the second occurrence of "went" and the second and third occurrences of "to" from "Sam went went to to to his business" to get "Sam went to his business".
// 3. We remove the second occurrence of "is", the second occurrence of "the", and the second occurrence of "eye" from
// "Reya is is the the best player in eye eye game" to get "Reya is the best player in eye game".
// 4. The sentence "in inthe" has no repeated words, so we do not modify it.
// 5. We remove the second occurrence of "ab" from "Hello hello Ab aB" to get "Hello Ab".
// 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.
//
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DuplicateWords {
public static void main(String[] args) {
String regex = "\\b([a-z]+)\\b(?:\\s+\\1\\b)+";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Scanner in = new Scanner(System.in);
int numSentences = Integer.parseInt(in.nextLine());
while (numSentences-- > 0) {
String input = in.nextLine();
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);
}
in.close();
}
}