Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

Do not use static variables to implement recursive methods. 

USING JAVA:

// P6

        public static int countSubstrings(String s1, String s2) {

        }

  • Write a recursive method countSubstrings that counts the number of non-overlapping occurrences of a string s2 in a string s1. Use the method indexOf() from String class.

 

  • As an example, with s1=”abab” and s2=”ab”, countSubstrings returns 2. With s1=”aaabbaa” and s2=”aa”, countSubstrings returns 2. With s1=”aabbaa” and s2=”AA”, countSubStrings returns 0.

Show the output on the following strings:

  • s1 = “Java is a programming language originally developed by James Gosling at Sun Microsystems  and released in 1995 as a core component of Sun Microsystems Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. Java is a general-purpose, concurrent, class-based, object-oriented language that is specifically designed to have as few implementation dependencies as possible. Java is currently one of the most popular programming languages in use, particularly for client-server web applications, with a reported 10 million users.”

 

  • s2= “Java”.
Expert Solution
Check Mark
Step 1

code : -

 

public class SubstringCounter {
    public static int countSubstrings(String s1, String s2) {
    // base case: s1 is empty or s2 is longer than s1
    if (s1.isEmpty() || s2.length() > s1.length()) {
        return 0;
    }
    
    // find the index of the first occurrence of s2 in s1
    int index = s1.indexOf(s2);
    
    // if s2 is not found in s1, return 0
    if (index == -1) {
        return 0;
    }
    
    // increment the count by 1 and recursively call countSubstrings
    // on the substring of s1 that starts after the first occurrence of s2
    // and the same s2 string
    return 1 + countSubstrings(s1.substring(index + s2.length()), s2);
}


    public static void main(String[] args) {
        String s1 = "Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. Java is a general-purpose, concurrent, class-based, object-oriented language that is specifically designed to have as few implementation dependencies as possible. Java is currently one of the most popular programming languages in use, particularly for client-server web applications, with a reported 10 million users.";
        String s2 = "Java";
        int count = countSubstrings(s1, s2);
        System.out.println("Number of non-overlapping occurrences of \"" + s2 + "\" in \"" + s1 + "\": " + count);
        
        
        // second test case 
        s1="abab";
        s2="ab";
        count = countSubstrings(s1, s2);
        System.out.println("\n\n\nNumber of non-overlapping occurrences of \"" + s2 + "\" in \"" + s1 + "\": " + count);
    }
}

 

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education