Hallo,
ich hab jetzt schon lange gesucht und finde keine Lösung im Netz
Folgendes Problem ich habe zum Beispiel einen String der Lautet "aabndmggmvndaasdfsdf"
hier kommt das aa doppelt vor und gg nur einmal.
wie kann ich harausfinden wie oft ein doppelter wert vorkommt? also dass dann auf der konsolo sowas wie: aa kommt 2 mal vor
gg kommt 1 mal vor
im Voraus herzlichen Dank für eure Hilfe
Hallo grubewol,
ein doppelter wert
Meinst du damit explizit zwei hinteranderfolgende gleiche Zeichen?
Das könnte ganz naiv so gelöst werden:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> counts = GetDoubledCharsCount("aabndmggmvndaasdfsdf");
Console.WriteLine("Group\tCount");
foreach (var kvp in counts.OrderByDescending(k => k.Value))
{
Console.WriteLine($"{kvp.Key}\t{kvp.Value}");
}
}
private static Dictionary<string, int> GetDoubledCharsCount(string text)
{
var counts = new Dictionary<string, int>();
for (int i = 1; i < text.Length; ++i)
{
if (text[i] == text[i - 1])
{
string key = text.Substring(i - 1, 2);
if (counts.TryGetValue(key, out int count))
{
counts[key] = count + 1;
}
else
{
counts.Add(key, 1);
}
}
}
return counts;
}
}
}
Die Aufgabe verallgemeinert auf beliebige Längen --> Longest common subsequence problem
mfG Gü
Stellt fachliche Fragen bitte im Forum, damit von den Antworten alle profitieren. Daher beantworte ich solche Fragen nicht per PM.
"Alle sagten, das geht nicht! Dann kam einer, der wusste das nicht - und hat's gemacht!"