Java Regex 2 - Duplicate Words

Sort by

recency

|

385 Discussions

|

  • + 0 comments

    public static void main(String[] args) {

        String regex = "\\b(\\w+)(?: +\\1)+\\b";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    
        Scanner in = new Scanner(System.in);
        int numSentences = Integer.parseInt(in.nextLine());
        int i=0;
        while (i<numSentences) {
            String input = in.nextLine(); 
            Matcher m = p.matcher(input);
            input = m.replaceAll("$1");
    
            // Prints the modified sentence.
            System.out.println(input);
            i++;
        }
    
        in.close();
    }
    
  • + 0 comments

    Java 8

    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);
                
                while (m.find()) {
                    input = input.replaceAll(m.group(), m.group(1));
                }
                
                System.out.println(input);
            }
            
            in.close();
    
  • + 1 comment
    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+)(\\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(regex, "$1");
                }
                
                // Prints the modified sentence.
                System.out.println(input);
            }
            
            in.close();
        }
    }
    
  • + 0 comments

    Here is Java Regex 2 - Duplicate Words solution - https://programmingoneonone.com/hackerrank-java-regex-2-duplicate-words-solution.html

  • + 0 comments

    Hiring a local Oklahoma City mold removal company means you're working with professionals who understand the specific challenges of the region. They are familiar with common causes of mold in the area—such as heavy spring rains, tornado-related water intrusion Check this out, and high humidity in the summer—and can tailor their services accordingly. Local companies also tend to offer faster response times, which is crucial when dealing with mold issues that can worsen rapidly.