Java Programming Question

timer Asked: Jan 21st, 2016

Question description

In Java. I have the following two questions I'm having trouble with.


Write a method where the signature is:

public static int subStrC(String s, char c)

Which receive a string 's' and a char 'c' and returns how many sub string has in 's' that ends in the char 'c' and they have inside them exactly one time the char 'c'

for example. for the given string s="abcbcabcacabcc" and the char c='c' the result would be 4

(because the sub strings are "cabcc","cacabc","cabcac","cbcabc")

No need to return the sub strings. just the number of sub strings.


Write a method where the signature is:

public static int subStrMaxC(String s, char c, int k) 

That gets the string 's', the char 'c' and a positive number 'k' and returns how many sub strings 's' has that starts and ends in the char 'c' and Inside them maximum 'k' times the char 'c'

No need to return the sub strings - just their numbers.


If the string is: s="abcbc" and char 'c'='c' and the number k=0 the result is 1. (sub string is "CBC")

If the string is: s="abcbcabcacab" and char 'c'='c' and number k=3 the result is 6 again (sub strings are "cbcabcac", "cabcac", "cbcabc", "cac", "cabc", "cbc" and there is no sub string s that starts with char 'c' and ends in the char 'c' and has 3 'c' chars).

If the strings is s="abc", char c='c' and number k=2 the result is 0. there is no sub strings that starts and ends in char 'c'.

The methods needs to be as efficient as possible.

If the method complexity would be higher then required not all points will be given.

When solving those methods we can only use all the methods in the String class and in compare methods (optional) -

public char charAt(int i) - returns the char at the place i for the string it operated on
public int length() - returns the length of the string. 

Tutor Answer

(Top Tutor) Studypool Tutor
School: New York University
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors