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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:31:34+00:00 2026-06-19T00:31:34+00:00

More formally, let str be the string in question, and let its length be

  • 0

More formally, let str be the string in question, and let its length be l. I am aware that the above can easily be accomplished using the substr function as follows:

  • Removing the first character – str.substr(1)
  • Removing the last character – str.substr(0,l-1)

But according to this page, the above method does so in O(l).

Is there a way to achieve the same in O(1)?

Edit: Before you mark this question as a duplicate, note that I am asking for an O(1) implementation for removing the terminal characters of a string. None of the answers to question of which this one is seemingly a duplicate of make any effort to answer this, apparently because that question doesn’t ask for it.

  • 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-19T00:31:36+00:00Added an answer on June 19, 2026 at 12:31 am

    The substr function doesn’t remove characters. The string you call that function on does not get modified, so all characters remain intact. Nonetheless, the time required to call it to select a substring that includes all but a terminal character will be O(n) because it involves making a copy of all the other characters.

    The time required for removing a character from a string lies in shifting all the subsequent characters to replace the one removed, much like the copying that occurs in substr. There are in general n characters in a string, so the complexity of removing an arbitrary character is O(n).

    There is no constant-time way to remove the first character of a string of arbitrary length. Removing a character that’s a fixed number of characters C from the end of a string can be done in constant time, so removing the final character (C = 0) is O(1).

    If you frequently need to add or remove elements from the termini of a sequence, you might consider a data structure more suited to that operation. Both list and deque are good for that.

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

Sidebar

Related Questions

In wikipedia's definition of command query separation , it is stated that More formally,
I have a number of tracks recorded by a GPS, which more formally can
More specific where do I attach OnGestureListener so that I can detect onSingleTapUp everywhere
JavaDoc defines set as : A collection that contains no duplicate elements. More formally,
how do I select a subset of points at a regular density? More formally,
More C++ learning questions. I've been using vectors primarily with raw pointers with a
More of a general MATLAB question than looking for programming advice -- if I
More of a design/conceptual question. At work the decision was made to have our
I was wondering if there is one or more strings that cannot be losslessy
More specifically, what is the authoritative source for that information? This may look like

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.