FRom hackerrank
You are given a string containing characters and only. Your task is to change it into a string such that there are no matching adjacent characters. To do this, you are allowed to delete zero or more characters in the string.
Your task is to find the minimum number of required deletions.
For example, given the string , remove an at positions and to make in deletions.
Solution in C# -
class Solution {
static int alternatingCharacters(string s) {
int deletions = 0;
for(int i=0; i<s.Length-1; i++)
{
if(s[i] == s[i+1])
{
deletions++;
}
}
return deletions;
}
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 = alternatingCharacters(s);
textWriter.WriteLine(result);
}
textWriter.Flush();
textWriter.Close();
}
}
Post a Comment