Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Sunday, May 13, 2018

Replace Pattern by X


Problem :

Given a string and a pattern, Replace all the continuous occurrence of pattern with a single X in the string

Code:


import java.util.*;
import java.util.regex.*;
import java.lang.*;
import java.io.*;
class replacePatternX
 {
  public static String replacePattern(String str,String pat)
  {
    Pattern p=Pattern.compile("("+pat+")+");
    Matcher m=p.matcher(str);
    return m.replaceAll(String.valueOf('X'));
  }
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
    System.out.println(replacePattern(ab.next(),ab.next()));
}
}
}

0 Comments:

Post a Comment

Stats