Persistent Systems Interview Question
Java DevelopersCountry: India
Interview Type: Written Test
It's hard to understand the problem definition. Does the string we look for need to be at the beginning of the strings? What are we trying to optimize?
So, supposing that we are looking only for prefixes, `input[]` is huge and we are optimizing for the fast counting of prefixes, you could save `input` into a trie and keep in the trie nodes the amount of strings with this prefix.
String x,y;
String[] a={"Ab", "cd", "f", "aB", "ef", "abc"};
int i,j,count=0;
Scanner s = new Scanner(System.in);
System.out.println("Enter the String/Sub-string to be searched");
x=s.nextLine();
System.out.println("The String to be searched is: "+x);
j= a.length;
for(i=0;i<j;i++)
{
y=a[i];
if(y.toLowerCase().contains(x.toLowerCase()))
{
count++;
}
}
System.out.println("There are "+count+" occurences in the given string");
import java.util.regex.*;
import java.util.*;
class Demo
{
public static void main(String args[])
{
String input[]={"ab","cd","f","ab","ef","abc"};
Scanner scan=new Scanner(System.in);
System.out.println("ENTER STRING WHICH U WANT TO SEARCH:");
String search=scan.next();
Pattern p=Pattern.compile(search);
int count=0;
for(String str:input)
{
Matcher m=p.matcher(str);
while(m.find())
{
count++;
}
}
System.out.println("OCCURENCE OF GIVEN STRING:"+count);
}
}
import java.util.regex.*;
import java.util.*;
class Demo
{
public static void main(String args[])
{
String input[]={"ab","cd","f","ab","ef","abc"};
Scanner scan=new Scanner(System.in);
System.out.println("ENTER STRING WHICH U WANT TO SEARCH:");
String search=scan.next();
Pattern p=Pattern.compile(search);
int count=0;
for(String str:input)
{
Matcher m=p.matcher(str);
while(m.find())
{
count++;
}
}
System.out.println("OCCURENCE OF GIVEN STRING:"+count);
}
}
- NoOne October 07, 2016