pythonatSheriff
Full Stack Web Developer
The problem is :
((Have the function MathChallenge(str) take the str parameter being passed and determine if there is some substring K that can be repeated N > 1 times to produce the input string exactly as it appears. Your program should return the longest substring K, and if there is none it should return the string -1.
For example: if str is "abcababcababcab" then your program should return abcab because that is the longest substring that is repeated 3 times to create the final string. Another example: if str is "abababababab" then your program should return ababab because it is the longest substring. If the input string contains only a single character, your program should return the string -1. ))
My solutions is :
Now, I feel like it can be done using (Linq) methods, although I read most of the methods, or there is a simpler solution, so I would like to know to increase my knowledge.
Thanks in advance.
((Have the function MathChallenge(str) take the str parameter being passed and determine if there is some substring K that can be repeated N > 1 times to produce the input string exactly as it appears. Your program should return the longest substring K, and if there is none it should return the string -1.
For example: if str is "abcababcababcab" then your program should return abcab because that is the longest substring that is repeated 3 times to create the final string. Another example: if str is "abababababab" then your program should return ababab because it is the longest substring. If the input string contains only a single character, your program should return the string -1. ))
My solutions is :
my solution to math problem:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class MainClass
{
public static void Main(string[] arg1)
{
//abcababcababcab
string str = "abababababab";
int count = 0;
for(var i=0; i<str.Length; i++)
{
count = count + 1;
}
Console.WriteLine(count);
int division = 0;
if(count%2 == 0)
{
//Console.WriteLine("can divide on 2");
division = 2;
}
else if(count%3 == 0)
{
//Console.WriteLine("can divide on 3");
division = 3;
}
char[] LooseLetters = str.ToCharArray();
int LettersCount = (LooseLetters.Length)/(division);
StringBuilder part1 = new StringBuilder(str);
if(division == 2)
{
part1.Insert(LettersCount, ",");
Console.WriteLine(part1);
}
else if(division == 3)
{
part1.Insert(LettersCount, ",");
part1.Insert((2*LettersCount)+1, ",");
Console.WriteLine(part1);
}
string strConverter = part1.ToString();
List<string> DuplicateString = strConverter.Split(",").ToList();
// foreach(var item in DuplicateString)
// {
// Console.WriteLine(item);
// }
string result = "";
if(division == 2)
{
if(DuplicateString[0] == DuplicateString[1])
result = DuplicateString[0];
else
result = "-1";
}
else if(division == 3)
{
if(DuplicateString[0] == DuplicateString[1] && DuplicateString[0] == DuplicateString[2])
result = DuplicateString[0];
else
result = "-1";
}
Console.WriteLine(result);
}
}
Now, I feel like it can be done using (Linq) methods, although I read most of the methods, or there is a simpler solution, so I would like to know to increase my knowledge.
Thanks in advance.