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 8128223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:48:28+00:00 2026-06-06T07:48:28+00:00

I am in the process of learning about big oh notation. For the code

  • 0

I am in the process of learning about big oh notation. For the code below, I have a program that counts the number of words, skipping over the delimiters. For this algorithm, the for loop would be for the length of the sentence and then there is contains that iterates to the string as well. So according to me the the big oh notation for this algorithm would be O(n^3). Is that correct or am I missing something about big Oh notation ?

public class wordCount {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String sentence = "How are you,zak;far: mon. day ?:";
    int count = 1;
    ArrayList<Character> delim = new ArrayList<Character>();
    delim.add(' ');
    delim.add(',');
    delim.add('.');
    delim.add(':');
    delim.add(';');
    delim.add('"');
    delim.add('\'');
    for (int i = 0; i != sentence.length() - 1; i++) {
        if (delim.contains(sentence.charAt(i))) {
            if (!delim.contains(sentence.charAt(i + 1))) {
                count++;
            }
        }
    }
    System.out.println("The count is: " + count);
}
}
  • 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-06-06T07:48:29+00:00Added an answer on June 6, 2026 at 7:48 am

    No, you’re not quite right – you’re assuming that the “n” of the O(n) of contains is the same as the “n” that you’re interested in. It’s not – it’s the length of the array list. In this case, that’s the number of delimiters, not the sentence length.

    So your algorithm is actually O(N * M) where N is the sentence length and M is the number of delimiters. If you view the set of delimiters as constant with only your sentence as input, your whole algorithm is O(N).

    Even though you call contains conditionally the second time, the total number of contains calls per iteration is only ever 1 or 2 – it can’t grow to (say) the number of delimiters or the size of the sentence.

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

Sidebar

Related Questions

I'm in the process of learning about simulated annealing algorithms and have a few
I'm in the process of learning C++. But there's so much more that I
I am in the process of learning the Yii Framework. I have been following
I'm learning about Thread Local Storage... (TLS) Here is my TLS Alloc code: //global
I've been learning about DRY code and my code isn't DRY... For example, I
I have a process that I am automating into a kind of GUI step
I'm in the process of learning more about C# and am wondering how to
I'm just learning about ORM's, and am wondering... if you have an object (a
I'm in the process of learning about servlet containers and web apps based on
I'm pretty new to java and really enjoying learning about it. I have made

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.