patternjavaMinor
Counting occurrences of a character in a string
Viewed 0 times
characterstringcountingoccurrences
Problem
Scenario "The Semantic Mind-Reader!" browser known as jhool is known
as mind-reader for a reason. Here's why:
You don't need to type 'www.' to open a website anymore. Though, you
still need to type '.com' to open a website. The browser predicts ALL
THE VOWELS in the name of the website. (Not '.com', though. Again!)
Obviously, this means you can type the name of a website faster and
save some time.
Input format: The first line contains tc, the number of test cases. The second line contains the name of websites, as a string.
Output format: You have to print the ratio of characters you would have typed in Jhool's browser, to your normal browser.
NOTE: You do NOT need to print the output in its lowest format. You
should print in its original fraction format. The names of all the
websites will be in small case only.
Every string will start from www. and end with .com, so well!
Sample Input
Sample Output
Explanation
Consider the first case: In Jhool's browser, you'll only have to type: ggl.com (7 characters) while in a normal browser, you'll have to
type www.google.com, which is 14 characters.
Here is my solution:
But the above took around 2.4583 sec in total for against the 7 set of input.
inpu
as mind-reader for a reason. Here's why:
You don't need to type 'www.' to open a website anymore. Though, you
still need to type '.com' to open a website. The browser predicts ALL
THE VOWELS in the name of the website. (Not '.com', though. Again!)
Obviously, this means you can type the name of a website faster and
save some time.
Input format: The first line contains tc, the number of test cases. The second line contains the name of websites, as a string.
Output format: You have to print the ratio of characters you would have typed in Jhool's browser, to your normal browser.
Constraints:
1 <= tc <= 100
1 <= Length of the website <= 200NOTE: You do NOT need to print the output in its lowest format. You
should print in its original fraction format. The names of all the
websites will be in small case only.
Every string will start from www. and end with .com, so well!
Sample Input
2
www.google.com
www.hackerearth.comSample Output
7/14
11/19Explanation
Consider the first case: In Jhool's browser, you'll only have to type: ggl.com (7 characters) while in a normal browser, you'll have to
type www.google.com, which is 14 characters.
Here is my solution:
import java.util.Scanner;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner keyboard=new Scanner(System.in);
int t=keyboard.nextInt();
keyboard.nextLine();
while(t!=0){
String s= keyboard.nextLine();
int len=s.length();
int count=0;
for(int i=4;i<len-4;i++){
if(s.substring(i,i+1).matches("[aeiouAEIOU]"))
count++;
}
System.out.println(len-count-4+"/"+len);
t--;
}
}
}But the above took around 2.4583 sec in total for against the 7 set of input.
inpu
Solution
So, performance. Let's list off things that are commonly done wrong that you do in your code:
The performance can be significantly improved by doing batch operations on the input, processing, and output.
Additionally, the last point is important. Java compiles code essentially on a method-by-method basis. By keeping all your code in the main method you are reducing the possibility for Java to compile and optimize it.
Read the whole file in, split the lines out in one operation, write all the results out as one string.
That will likely remove more than half your execution time... but your actual algorithm is slow as well.
Since you use regular expressions to split the characters, you may as well use one to just clean up the whole URL... and strip all vowels that way.
Here's a scratch-up of what I would consider given this task. It shows the ideas I was mentioning, and also uses some Java 8 features:
- print statements in a loop
- line-by-line parsing of input
- mid-list removals of values (
removeAll())
- not enough functions
The performance can be significantly improved by doing batch operations on the input, processing, and output.
Additionally, the last point is important. Java compiles code essentially on a method-by-method basis. By keeping all your code in the main method you are reducing the possibility for Java to compile and optimize it.
Read the whole file in, split the lines out in one operation, write all the results out as one string.
That will likely remove more than half your execution time... but your actual algorithm is slow as well.
Since you use regular expressions to split the characters, you may as well use one to just clean up the whole URL... and strip all vowels that way.
Here's a scratch-up of what I would consider given this task. It shows the ideas I was mentioning, and also uses some Java 8 features:
public static void main(String[] args) throws IOException {
for (String infile : args) {
System.out.println(processSource(Paths.get(infile)));
}
}
private static String processSource(Path path) throws IOException {
List alldata = Files.readAllLines(path);
Iterator it = alldata.iterator();
final int size = Integer.parseInt(it.next());
String[] results = new String[size];
for (int cnt = 0; it.hasNext() && cnt %s", clean.length() + url.length() - limit, url.length(), url);
// simple result
return String.format("%d/%d", clean.length() + url.length() - limit, url.length());
}Code Snippets
public static void main(String[] args) throws IOException {
for (String infile : args) {
System.out.println(processSource(Paths.get(infile)));
}
}
private static String processSource(Path path) throws IOException {
List<String> alldata = Files.readAllLines(path);
Iterator<String> it = alldata.iterator();
final int size = Integer.parseInt(it.next());
String[] results = new String[size];
for (int cnt = 0; it.hasNext() && cnt < size; cnt++) {
results[cnt] = computeRatio(it.next());
}
return String.join("\n", results);
}
private static final Pattern DISCARD = Pattern.compile("(^www\\.)|[aeiou]");
private static final String computeRatio(String url) {
int limit = url.lastIndexOf('.');
String clean = DISCARD.matcher(url.substring(0, limit)).replaceAll("");
// include the URL for debug:
// return String.format("%d/%d -> %s", clean.length() + url.length() - limit, url.length(), url);
// simple result
return String.format("%d/%d", clean.length() + url.length() - limit, url.length());
}Context
StackExchange Code Review Q#90841, answer score: 3
Revisions (0)
No revisions yet.