Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 825277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:13:45+00:00 2026-05-15T03:13:45+00:00

I have this method: public static int what(String str, char start, char end) {

  • 0

I have this method:

public static int what(String str, char start, char end)
{
    int count=0;
    for(int i=0;i<str.length(); i++) {
        if(str.charAt(i) == start)
        {
            for(int j=i+1;j<str.length(); j++)
            {
                if(str.charAt(j) == end)
                    count++;
            }
        }
    }
    return count;
}

What I need to find is:

1) What is it doing? Answer: counting the total number of end occurrences after EACH (or is it? Not specified in the assignment, point 3 depends on this) start.

2) What is its complexity? Answer: the first loops iterates over the string completely, so it’s at least O(n), the second loop executes only if start char is found and even then partially (index at which start was found + 1). Although, big O is all about worst case no? So in the worst case, start is the 1st char & the inner iteration iterates over the string n-1 times, the -1 is a constant so it’s n. But, the inner loop won’t be executed every outer iteration pass, statistically, but since big O is about worst case, is it correct to say the complexity of it is O(n^2)? Ignoring any constants and the fact that in 99.99% of times the inner loop won’t execute every outer loop pass.

3) Rewrite it so that complexity is lower.
What I’m not sure of is whether start occurs at most once or more, if once at most, then method can be rewritten using one loop (having a flag indicating whether start has been encountered and from there on incrementing count at each end occurrence), yielding a complexity of O(n).

In case though, that start can appear multiple times, which most likely it is, because assignment is of a Java course and I don’t think they would make such ambiguity.
Solving, in this case, is not possible using one loop… WAIT! Yes it is..!
Just have a variable, say, inc to be incremented each time start is encountered & used to increment count each time end is encountered after the 1st start was found:

inc = 0, count = 0
if (current char == start) inc++
if (inc > 0 && current char == end) count += inc

This would also yield a complexity of O(n)? Because there is only 1 loop.

Yes I realize I wrote a lot hehe, but what I also realized is that I understand a lot better by forming my thoughts into words…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:13:46+00:00Added an answer on May 15, 2026 at 3:13 am
    1. It increments `count` for every end character after any given start. So if there’s more than one start, it can increment it more than once for the same end.
    2. In the worst case, it will call charAt (n^2 – n) / 2 = ((n – 1) + (n – 2) + … + 1) times. That is O(n^2). This occurs when every character is start.
    3. If you were counting end characters after the first start, you could simply return count after the inner for. But since the number of times count is incremented depends on the number of starts, your final psuedo-code is on the right track. However, you need to switch the order of the ifs to deal with the special case where start == end. You also don’t need to check if inc > 0.
    inc = 0, count = 0
    
    if (current char == end) count += inc
    if (current char == start) inc++
    

    This is O(n) in all cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method: public static string ByteToString(byte[] Bytes, int Length) { Debug.Assert(Length
I have a page with this method in CreateTicket.aspx.cs: [WebMethod()] public static string Categories()
the ToSelectList method I have: public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty,
I have a method that looks like this: public static String escape(String text) {
I have this method call: public DataTemplate Create(Type type, string propertyName) { string str
I have this method to test : public static Date getDateSinceUTC(CstOrderBean orderBean) { int
I have this method: public static DateTime GetDatetime(string ampm, string hour, string minute) {
So I have a simple method called Invert : public static void Invert(this bool
I have this method: public void testJSNI2(){ String x = test; } I can
I have this method: public CampaignCreative GetCampaignCreativeById(int id) { using (var db = GetContext())

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.