Print Number containing Digit only 1 2 3
Idea is to use REGEX.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
class GFG
{
static boolean flag;
static List<String> lst=new ArrayList<String>();
public static void checkExpr(String str)
{
Pattern p=Pattern.compile(".*[^123].*");
Matcher m=p.matcher(str);
if(!m.find())
{
lst.add(str);
flag=true;
}
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
flag=false;
lst.clear();
for(int i=0;i<n;++i)
checkExpr(ab.next());
Collections.sort(lst);
for(String s:lst)
System.out.print(s+" ");
if(!flag)
System.out.print("-1");
System.out.println();
}
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
class GFG
{
static boolean flag;
static List<String> lst=new ArrayList<String>();
public static void checkExpr(String str)
{
Pattern p=Pattern.compile(".*[^123].*");
Matcher m=p.matcher(str);
if(!m.find())
{
lst.add(str);
flag=true;
}
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
flag=false;
lst.clear();
for(int i=0;i<n;++i)
checkExpr(ab.next());
Collections.sort(lst);
for(String s:lst)
System.out.print(s+" ");
if(!flag)
System.out.print("-1");
System.out.println();
}
}
}
0 Comments:
Post a Comment