Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Java Regex 2 – Duplicate Words-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java Regex 2 – Hacker Rank Solution
Java Regex 2 – Hacker Rank Solution
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, , denoting the number of sentences.
Each of the subsequent lines contains a single sentence consisting of English alphabetic letters and whitespace characters.
Constraints
- Each sentence consists of at most English alphabetic letters and whitespaces.
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
- We remove the second occurrence of
bye
and the second and third occurrences ofworld
fromGoodbye bye bye world world world
to getGoodbye bye world
. - We remove the second occurrence of
went
and the second and third occurrences ofto
fromSam went went to to to his business
to getSam went to his business
. - We remove the second occurrence of
is
, the second occurrence ofthe
, and the second occurrence ofeye
fromReya is is the the best player in eye eye game
to getReya is the best player in eye game
. - The sentence
in inthe
has no repeated words, so we do not modify it. - We remove the second occurrence of
ab
fromHello hello Ab aB
to getHello 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.
Java Regex 2 - Duplicate Words– Hacker Rank Solution
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(\\w+)(\\W+\\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(); } }