Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created April 10, 2016 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/09c77ba32b156f765b4debb2bccba0c8 to your computer and use it in GitHub Desktop.
Save jianminchen/09c77ba32b156f765b4debb2bccba0c8 to your computer and use it in GitHub Desktop.
string function calculation - HackerRank - score 0 out of 100 points - only pass one case "aaaaaa"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace stringFunctionCalculation
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
Console.WriteLine(getMaximumValue(s).ToString());
}
public static int getMaximumValue(string s)
{
if (s == null || s.Length == 0) return 0;
int n = s.Length;
int pValue = 1;
HashSet<string> set = new HashSet<string>();
for (int i = 1; i < n; i++) // substring length: i
{
int start = 0;
int end = i - 1;
while (start <= n - 1 && end <= n - 1)
{
string s1 = s.Substring(0, i);
if (!set.Contains(s1)) // check if set has string
{
set.Add(s1);
int current = i * getCountSubstring(s1, s);
if (current > pValue)
pValue = current;
}
start++;
end++;
}
}
return pValue;
}
/*
* Just go over O(n) - one loop to find the count value
*/
private static int getCountSubstring(string substring, string s)
{
int len1 = substring.Length;
int len = s.Length;
if (len1 > len) return 0;
int start = 0;
int end = len1 - 1;
int count = 0;
while(start <len && end < len)
{
if(substring.CompareTo(s.Substring(start, len1)) ==0)
count++;
start++;
end++;
}
return count;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment