Created
April 10, 2016 22:34
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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