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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:06:32+00:00 2026-06-12T11:06:32+00:00

I need to check the number of different characters in one string whose length

  • 0

I need to check the number of different characters in one string whose length can be as long as 20,000 and total test cases of <=10,000. I was doing it by replacing the other characters of the string and then checking its length as the below code shows:

 int no7=myString.replaceAll("[^7]","").length();
 int no0_3=myString.replaceAll("[^0-3]","").length();
 int no5_6=myString.replaceAll("[^56]","").length();

I was wondering how the replaceAll method works and whether it would be faster if I do the counting in one single loop checking each character of the string. Thanks in advance.

  • 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-12T11:06:34+00:00Added an answer on June 12, 2026 at 11:06 am

    First of all you can make the replacement much faster by adding a + after the character class (e.g. [^7]+). That will replace consecutive runs of unwanted characters instead of just a single one at a time. Depending on your input string this may gain you a significant performance boost.

    But in your case I wouldn’t really replace anything and check the length. What you want is the number of sevens, the number of digits between 0 and 3 and the number of fives and sixes. So just write a single loop that checks those:

    int no7 = 0, no0_3 = 0, no5_6 = 0;
    for (int i = 0; i < myString.length(); i++) {
      char c = myString.charAt(i);
      if (c == '7') no7++;
      if (c >= '0' && c <= '3') no0_3++;
      if (c == '5' || c == '6') no5_6++;
    }
    

    This will be faster because you don’t have to construct three individual strings to check their lengths and throw them away again and you also save on the regex construction, parsing and runtime. A simple iteration over every character (which is what the regex must do anyway) therefore cuts down your time to at most a third of the original runtime, if not more.

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

Sidebar

Related Questions

I need to lower my received sequence number for performing FIX certification to check
I'm trying to count the number of characters that occur in a java string.
I am writing a small app which I need to test with utf-8 characters
I need a sanity check here if we can, any ideas on correcting/changing the
I have a string with thousands of number in it. I need to go
I have a number of check boxes on my JSP page. I need to
What I need to do is loop over a large number of different files
testGroupList is a list of integer. I need to check the numbers in testGroupList
I need to check for a condition on each page I visit on the
I need to check for duplicates before saving to the database in the create

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.