Java Regex 2 - Duplicate Words

Sort by

recency

|

358 Discussions

|

  • + 0 comments

    none of my submitted codes worked, even though the result was the same as expected, and even this code didn't work, unless it was exactly the same as many user submit here. wtf?!

  • + 0 comments

    code give right output but still wrong why???

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

    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 regex = "\\b(\\w+)(\\s+\\1\\b)+";
    
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    
        Matcher m = p.matcher(input);
        String output = m.replaceAll("$1");
    
        System.out.println(output);
    }
    
    in.close();
    

    } }

  • + 0 comments

    I have this code which gives exactly the expected result but still the test cases are failing.

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

    public class Solution {

    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 regex = "\\b(\\w+)(\\s+\\1\\b)+";
    
            Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    
            Matcher m = p.matcher(input);
            String output = m.replaceAll("$1");
    
            System.out.println(output);
        }
    
        in.close();
    }
    

    }

  • + 0 comments

    something is wrong, not able to output anything import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        String regex = "\\b(\\w+)\\b(?:\\s+\\b\\1\\b)+";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Scanner scan = new Scanner(System.in);
        int count = scan.nextInt();
        scan.nextLine();
        for(int i = 0; i< count; i++){
            String s = scan.nextLine();
            System.out.println(s);
            String smallCase = s.toLowerCase();
            Matcher matches = p.matches(smallCase);
            String result = matches.replaceAll("$1");
            System.out.println(result);
            }
        }
    

    }

  • + 0 comments

    what wrong in this code ? public static void main(String[] args) {

         Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());;
        String regex = "\\b(\\w+)(?:\\s+\\1\\b)+";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        while (n > 0) {
            String input = scanner.nextLine();
            Matcher matcher = pattern.matcher(input);
            String resultTmp = matcher.replaceAll("$1");
            System.out.println(resultTmp);
            n--;
        }
        scanner.close();
    }