mark.nag
BAN USERif (String.IsNullOrEmpty(str))
{
return "";
}
string ret = str.Substring(0, 1);
for (int i = 0; i < str.Length; i++)
{
//handel even
int max = i;
int min = i;
while (max < str.Length && min >= 0 && str[max] == str[min])
{
min--;
max++;
}
min++;
max--;
string p1 = "";
p1 = str.Substring(min, max - min + 1);
if (p1.Length > ret.Length)
{
ret = p1;
}
//handle odd
max = i + 1;
min = i;
while (max < str.Length && min >= 0 && str[max] == str[min])
{
min--;
max++;
}
string str2 = "";
min++;
max--;
if (max - min > 0)
{
str2 = str.Substring(min, max - min + 1);
}
if (str2.Length > ret.Length)
{
ret = str2;
}
}
return ret;
public static string findMaxPlaindrome(string str)
{
if (String.IsNullOrEmpty(str))
{
return "";
}
string ret = "";
int maxLenght = 0;
for (int i = 0; i < str.Length; i++)
{
int max = i;
int min = i;
while (max < str.Length && min >= 0 && str[max] == str[min])
{
min--;
max++;
}
min++;
max--;
if (max - min > maxLenght)
{
maxLenght = max - min;
ret = str.Substring(min, max - min + 1);
}
}
return ret;
}
- mark.nag June 01, 2016public static List<List<string>> AnagramsWords(List<string> words)
{
List<List<string>> ret = new List<List<string>>();
while (words.Count > 0)
{
List<string> temp = new List<string>();
temp.Add(words[0]);
for (int j = 1; j < words.Count; j++)
{
if (AreAnagrams(words[0], words[j]))
{
temp.Add(words[j]);
}
}
for (int k = 0; k < temp.Count; k++)
{
words.Remove(temp[k]);
}
ret.Add(temp);
}
return ret;
}
public static bool AreAnagrams(string word1, string word2)
{
if (word1.Length != word2.Length)
{
return false;
}
Dictionary<char, int> count = new Dictionary<char, int>();
List<char> word1Lettres = word1.ToList<char>();
word1Lettres.Sort();
List<char> word2Lettres = word2.ToList<char>();
word2Lettres.Sort();
for (int i = 0; i < word1Lettres.Count; i++)
{
if (word1Lettres[i] != word2Lettres[i])
{
return false;
}
}
return true;
}
- mark.nag June 12, 2016