sanjeevakumar
BAN USERprivate static void PrintDiagonal(int[][] input)
{
int totalLines = input.Length * 2 - 1;
for (int line = 0; line < totalLines; line++)
{
Console.WriteLine();
int row = line >= input.Length ? input.Length - 1 : line;
for (int column = line - row; row < input.Length && row >= 0 && column >= 0 && column < input.Length; row--, column++)
{
Console.Write(" " + input[column][row]);
}
}
}
private static int CountValid(string s)
{
int count = 0;
if (string.IsNullOrWhiteSpace(s)) return 0;
if (s.IndexOf('0') == -1)
count++; //one valid interpretation with all single digit numbers transformed to chars 'a' - 'i'
for (int i = 0; i < s.Length - 1; i++)
{
int twoCharInterpretation = GetInt(s, i);
if (twoCharInterpretation != 10 // since 10 can only have one interpretation => j
&& twoCharInterpretation != 20 // since 20 can only have one interpretation => t
&& twoCharInterpretation > 9 // 06 cannot be valid interpretation since it is either part of 10 or 20
&& twoCharInterpretation <= 26)
{
count++;
}
if (twoCharInterpretation % 10 == 0 && twoCharInterpretation > 26)
{
return 0;// like 11301 334508 are invalid 10, 20 can be valid, but 30 is not
}
}
return count;
}
private static int GetInt(string s, int index)
{
string twoChars = string.Empty;
twoChars = twoChars + s[index] + s[index + 1];
return int.Parse(twoChars);
}
Time O(nlogn) space => O(1)
- sanjeevakumar February 10, 2013