Java Regex 2 - Duplicate Words

Sort by

recency

|

366 Discussions

|

  • + 1 comment

    Same with me

    import java.util.Scanner;
    
    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();    
            String[] arr = input.split(" ");
            String[] res = new String [arr.length];
            int i = 0;
            for(String item : arr ){                
                boolean found = false;
                    
                for (String  term :  res )
                    if ( term != null && term.equalsIgnoreCase(item))
                        found = true;
                    
                if ( found == false )
                    res[i] = item;
                i++;
            }
            
            input = "";        
            for (String  term : res )
                if (term != null)
                    input += term + " ";
                
            System.out.println(input);
        }
        in.close();
    }
    }
    
  • + 0 comments

    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();
    }
    

    }

  • + 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(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