I've got an interview coming up for which the following is a prospective question:
Given 2 strings, for instance:
- John Lennon
- Paul ON
delete every character from string 1 which corresponds with any character in string 2 (case exclusive).
So following the anticipated manipulations, the output in our case would be:
Jhe
I've devised the following program for the same:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.printf("Enter 2 strings%n"); String s1,s2,s3; s1=br.readLine(); s2=br.readLine(); s3="["; for(int j=0;j<s2.length();j++) { s3+=Character.toLowerCase(s2.charAt(j)); s3+=Character.toUpperCase(s2.charAt(j)); } s3+="]"; Pattern p = Pattern.compile(s3); Matcher m = p.matcher(s1); int[] a = new int[10]; int k=0; while(m.find()) { a[k]=m.start(); k++; } int j=0; for(int i=0;i<k;i++) { System.out.print(s1.substring(j, a[i])); j=a[i]+1; } System.out.print(s1.substring(j));
This works seamlessly, but my predicament is that these folks don't want you to just get a bloody veracious output, they want a highly optimized output i.e. spatial and temporal requirements as to your program should to be as minimum as can be. And mine is nowhere near the epitome of the optimal solution
If anybody feels that they can alter my code or come up with something better that'd be optimal, I'd be much obliged!