FRom hackerrank
Amanda has a string of lowercase letters that she wants to copy to a new string. She can perform the following operations with the given costs. She can perform them any number of times to construct a new string :
- Append a character to the end of string at a cost of dollar.
- Choose any substring of and append it to the end of at no charge.
Given strings , find and print the minimum cost of copying each to on a new line.
For example, given a string , it can be copied for dollars. Start by copying , and individually at a cost of dollar per character. String at this time. Copy to the end of at no cost to complete the copy.
Solution in C# -
class Solution {
static int stringConstruction(string s) {
string p = "";
for(int i=0;i<s.Length;i++)
{
if(!p.Contains(s[i]))
p = p+s[i];
}
return p.Length;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int q = Convert.ToInt32(Console.ReadLine());
for (int qItr = 0; qItr < q; qItr++) {
string s = Console.ReadLine();
int result = stringConstruction(s);
textWriter.WriteLine(result);
}
textWriter.Flush();
textWriter.Close();
}
}
Post a Comment