waged7
BAN USER/*I'm sure this can be optimized further but I'm pasting it for others to get ideas from
The idea here is finding segments in the master string that contain all the characters of the alphabet string(to remove) and then shrinking the master string 1 character at a time*/
class Program
{
static void Main(string[] args)
{
String str = "aabbccba";
String ab = "abc";
StringBuilder sbr = new StringBuilder(str);
//Console.WriteLine(str.Substring(0));
for (int i = 0; i < sbr.Length; i++)
{
FindSubstring(str.Substring(i), ab);
FindSubstring(str.Substring(0, str.Length - i),ab);
}
//FindSubstring(str, ab);
//sbr.Remove(1, 1);
Console.WriteLine("{0}", sbr.ToString());
Console.ReadLine();
}
public static void FindSubstring(String str, String ab)
{
StringBuilder sbr = new StringBuilder(str);
StringBuilder abSbr = new StringBuilder(ab);
int wordStart = 0;
for(int i = 0; i < sbr.Length; i++)
{
for(int x = 0;x<abSbr.Length;x++)
{
char iChar = sbr[i];
char xChar = abSbr[x];
if (iChar==xChar)
{
abSbr.Remove(x, 1);
break;
}
}
if (abSbr.Length == 0)
{
Console.WriteLine(sbr.ToString(wordStart, i - wordStart+1));
abSbr = new StringBuilder(ab);
wordStart = i;
}
}
}
}
static void printDiag(int [,] mtx)
{ int x = mtx.GetLength(0)-1;
int y = 0;
while(x<= mtx.GetLength(0)-1 && y<= mtx.GetLength(1)-1){
int ydim = y;
int xdim = x;
while(xdim <= mtx.GetLength(0)-1 && ydim <= mtx.GetLength(1) - 1)
{
Console.Write(mtx[xdim, ydim]);
Console.Write(" ");
xdim++;
ydim++;
}
Console.WriteLine();
if (x > 0)
{
x--;
}
if(ydim > mtx.GetLength(0) - 1)
{
y++;
}
}
}
- waged7 March 12, 2017