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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:26:42+00:00 2026-05-20T18:26:42+00:00

For this question, a pair in a string is defined as a situation where

  • 0

For this question, a “pair” in a string is defined as a situation where two instances of one character are separated by another character. So in “AxA” the A’s make a pair. Pairs can overlap, so “AxAxA” contains three pairs; two for A and one for x.

Further examples:

countPairs(“axa”) → 1
countPairs(“axax”) → 2
countPairs(“axbx”) → 1

I was asked how to compute the number of pairs in a given string in an interview yesterday, and I’m not sure how to do 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-05-20T18:26:43+00:00Added an answer on May 20, 2026 at 6:26 pm

    An O(n) solution would be to iterate the string (from 0 to length-2) and (using charAt(..)) to verify whether the current character is equal to the current+2. If so, increment a pairsCount variable

    int pairsCount = 0;
    for (int i = 0; i < str.length() - 2; i ++) {
       if (str.charAt(i) == str.charAt(i + 2)) {
          pairsCount ++;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is similar in concept to this one , except I see I
This question is related to another question I wrote: Trouble using DOTNET from PHP.
This question is related to the one I asked here . I'm trying to
I apologize if this is a dumb question, but hear me out: Dictionary<string, string>
I have many string pairs and one large string (which is the content of
First, I know my question would be possibly duplicate of this one , but
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
This question is inspired from another topic which poses this question: Find the first
In an pre-interview, I am faced with a question like this: Given a string
This question is directly related to this SO question I posed about 15 minutes

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.