Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

below i have this java find my cat algorithm, and it has a run time of o(n^2), now how can i change the implementation to

below i have this java find my cat algorithm, and it has a run time of o(n^2), now how can i change the implementation to have a faster run time of o(n)

please explain what you did,

thanks

my code:

1 public class findcat 2 { 3 static void countCat(String str) 4 { 5 int count=0; 6 7 // search for pattern "((" 8 for (int i = 0; i < str.length() - 1; i++) 9 { 10 if (str.substring(i, i + 2).equals("((")) 11 { 12 13 // Search for pattern "))" if found "((" in outer loop 14 for (int j=i+2; j < str.length() - 1;j++){ 15 if (str.substring(j, j + 16 2).equals("))")){ 17 count=count+1; 18 } 19 } 20 } 21 } 22 23 System.out.println(count); 24 } 25 26 27 public static void main(String[] args) 28 { 29 String str1 = "(())(())(()"; 30 countCat(str1); 31 } 32 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions