5

What are the best algorithms available to find longest repeating patterns of characters in a string using .net?

Benjamin's user avatar
Benjamin
12k13 gold badges75 silver badges120 bronze badges
askedMar 15, 2011 at 12:25
Jayantha Lal Sirisena's user avatar
4
  • 3
    You will have to be more specific. Maybe give some examples. Also, have you done any research yourself already? If so, tell us what you found.CommentedMar 15, 2011 at 12:28
  • 1
    you can build a dictionary, like what lossless data compression algorithms do. See for exampleen.wikipedia.org/wiki/Dictionary_coderCommentedMar 15, 2011 at 12:40
  • 1
    your 'edit' does not make it any less vague, IMO.Try to do what Space_C0wb0y suggest.CommentedMar 15, 2011 at 13:02
  • 1
    I'm unsure why you want thelongest pattern. Clearly for the string "abcabca" the pattern "abc" is more interesting than "abcabc"CommentedMar 15, 2011 at 13:44

2 Answers2

4

I guess that you speak aboutpattern discovery. Take a look at some elementary aproach (source)

private static Dictionary<string, int> FindPatterns(string value) {  List<string> patternToSearchList = new List<string>();  for (int i = 0; i < value.Length; i++) {    for (int j = 2; j <= value.Length / 2; j++) {      if (i + j <= value.Length) {        patternToSearchList.Add(value.Substring(i, j));      }    }  }  // pattern matching  Dictionary<string, int> results = new Dictionary<string, int>();  foreach (string pattern in patternToSearchList) {    int occurence = Regex.Matches(value, pattern, RegexOptions.IgnoreCase).Count;    if (occurence > 1) {      results[pattern] = occurence;    }  }  return results;}static void Main(string[] args) {  Dictionary<string, int> result = FindPatterns("asdxgkeopgkajdflkjbpoijadadafhjkafikeoadkjhadfkjhocihakeo");  foreach (KeyValuePair<string, int> res in result.OrderByDescending(r => r.Value)) {    Console.WriteLine("Pattern:" + res.Key + " occurence:" + res.Value.ToString());  }  Console.Read();}

The algorithm consist of 2 stages.

  • Choose pattern
  • Find pattern in input string (Algorithm ofpattern matching)

It is used Regex for pattern matching. There are other more advanced algorithms. These algorithms are enlisted on addresshttp://www-igm.univ-mlv.fr/~lecroq/string/However, code samples are written in C. Also you'd take a look onBoyer-Moore algorithm for pattern matching, written in C#

answeredMar 15, 2011 at 13:31
Oleg Svechkarenko's user avatar
Sign up to request clarification or add additional context in comments.

2 Comments

I thinks this is kind of brute force method. Are there algorithms to do this using dynamic programming or some other method ?
Yes, it's brute force method. I expanded my answer.
1

Pseudocode:

For N=1 to InputString.Length-1  rotatedString = RotateStringByN(InputString,N)  For N=0 to InputString.Length-1     StringResult[N] = if (rotatedString[N]==InputString[N]) then                            InputString[N]                         else                             Convert.ToChar(0x0).ToString()  RepeatedStrings[] = String.Split(StringResult, Convert.ToChar(0x0).ToString())  SaveLongestStringFrom(RepeatedStrings)

... Or just look here atSO thread for other solutions.

answeredMar 16, 2011 at 9:56
Agnius Vasiliauskas's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.