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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:20:37+00:00 2026-05-30T21:20:37+00:00

A string = aabbccaaabbcbbdbabdaaa; How can that string be checked out in efficient way

  • 0

A string = "aabbccaaabbcbbdbabdaaa";
How can that string be checked out in efficient way finding the inside string duplicates:
I mean:

  1. Looking for 2-letters string in string:

    aa = ” aa bbcc aa abbcbbdbabd aa a”;
    //NO whitespaces here or elsewhere in the string. Just added them to emphasize “aa”.
    aa = “aa bbcca aa bbcbbdbabda aa “;
    total aa = 5;
    distance between aa = 4,5,11,12;

    bb = “aa bb ccaaa bb c bb dbabdaaa”;
    total bb = 3;
    distance between bb = 5,1
    …

  2. Looking for 3-letters string in string:

    aaa = ” aaa bbcc aaa bbcbbdbabd aaa “;
    total aaa = 3;
    distance between aaa = 4,10;
    …

My attempt was in 4 cycles way and very slow.

P.S.
Any help is appreciated. Sorry for my English.

EDIT:
Sorry for bad question. I’ve forgotten to say that the string should also be checked for 4 chars duplicates and other-chars duplicates:
aabb = ” aabb cca aabb cbbdbabdaaa”;
total aabb = 2;
distance between aabb = 3;

EDIT 2:
The duplicates we are looking for should not be entered manually. Imagine that the string is 20k symbols and you’re searching for ANY duplicates (there no whitespaces) and the distance between those duplicates.
Thanks and sorry again for not correct question.

  • 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-30T21:20:38+00:00Added an answer on May 30, 2026 at 9:20 pm

    Here’s a solution in C#

    static Dictionary<string, List<int>> GetDuplicates2(string value)
    {
        var duplicates = new Dictionary<string, List<int>>();
        for (int i = 0; i < value.Length; i++)
        {
            for (int slength = 2; slength < (value.Length - i) / 2 + 2; slength++)
            {
                var littleString = value.Substring(i, slength);
    
                if (!duplicates.ContainsKey(littleString))
                {
                    int nextOccurrence = value.IndexOf(littleString, i + slength - 1);
    
                    if (nextOccurrence != -1)
                    {
                        var l = new List<int>();
                        l.Add(i);
                        l.Add(nextOccurrence);
                        duplicates.Add(littleString, l);
    
                        while ((nextOccurrence = value.IndexOf(littleString, nextOccurrence + slength - 1)) != -1)
                        {
                            duplicates[littleString].Add(nextOccurrence);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    
        return duplicates;
    }
    

    I wrote this according to your comment…

    list of all [2, 3, …, n/2] characters duplicates, where n = string
    length

    I think this is working pretty well. It returns a Dictionary containing the string and the indices of each duplicate. In terms of performance, calling IndexOf() so many times is probably the slowest part of this, but I don’t know any way around that.

    UPDATE
    I changed the code to include the overlapping requirement.

    UPDATE #2
    I added a couple conditions where the algorithm will break out of the inner for loop. This improves performance quite a bit (especially when there are few duplicates to be found).

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

Sidebar

Related Questions

string s = <script type=text/javascript src=/stepcarousel.js></script> How can I get the data inside the
string x; foreach(var item in collection) { x += item+,; } can I write
String a=aaa; String b=bbb; String c=ccc; String d=ddd; String p,q,r,s; How can I assign
string.lastIndexOf(searchValue[, fromIndex]) MDN says that fromIndex default value is equal to the string.length ,
String data=this we want append in that file; byte[] getbyte = data.getBytes(); outputstream.write(getbyte); outputstream.flush();
string = 'one a day' how do I remove words from the string that
String 2011-05-19 10:30:14 To Thu May 19 10:30:14 UTC 2011 How can I convert
String(255) vs String(99999) When designing MySQL Columns, Does it really matter? I mean...I have
String s = ; for(i=0;i<....){ s = some Assignment; } or for(i=0;i<..){ String s
string percentage = e.Row.Cells[7].Text; I am trying to do some dynamic stuff with my

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.