Java Regex 2 - Duplicate Words

Sort by

recency

|

365 Discussions

|

  • + 1 comment

    La salida de mi programa es igual a la esperada (verificadas con editores hex) pero el summit marca error.

    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()) {
                    String group = m.group();
                    String[] temp = group.split(" ");
                    input = input.replaceAll(group, temp[0]);
                    m = p.matcher(input);
                }
                
                // Prints the modified sentence.
                System.out.println(input);
            }
            
            in.close();
        }
    
  • + 0 comments

    Hi, this is my solution but always marks error, any advice?

    import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.Scanner;

    class Main{

    public static void main(String[] args) {
    
        // Regular expression to match repeated words
        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(m.group(), m.group(1));
            }
    
            // Prints the modified sentence.
            System.out.println(input);
        }
    
        in.close();
    }
    

    }

  • + 2 comments

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

    }

  • + 0 comments

    Java Regex 2 - Duplicate Words challenges users to identify and remove repeated words in a string using regular expressions. It tests proficiency in pattern matching and efficient text processing. Betbhai9 Register

  • + 0 comments

    Please advise on why test case fails even though output matches. I believe its an minor mistake.

    public class DuplicateWords {

    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        int numSentences = Integer.parseInt(in.nextLine());
    
        while(numSentences > 0)
        {
            String input = in.nextLine();
            List<String> list = Arrays.asList(input.split(" "));
            Set<String> ans = new HashSet<>();  
            String answer = list.stream()
            .filter(str -> ans.add(str.toLowerCase()))
            .collect(Collectors.joining(" ")).trim();        
            System.out.println(answer);
            numSentences--;
        }
    
        in.close();
    }
    

    }