Skip to content

Instantly share code, notes, and snippets.

@Tangrila-BG
Created June 6, 2017 20:54
Show Gist options
  • Save Tangrila-BG/4f73e65f7d706791a1bd1d02b2b02402 to your computer and use it in GitHub Desktop.
Save Tangrila-BG/4f73e65f7d706791a1bd1d02b2b02402 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Csharp.Advanced.ManualStringProcessing
{
public partial class ManualStringProcessing
{
public static class _15MelrahShake
{
public static void Solution()
{
var sentence = Console.ReadLine();
var pattern = Console.ReadLine();
// Console.WriteLine(ShakeRecursive(sentence, pattern));
while (true)
{
var firstIndex = sentence.IndexOf(pattern);
var secondIndex = sentence.LastIndexOf(pattern);
var canShake = firstIndex > -1
&& secondIndex > -1
&& pattern.Length > 0;
if (!canShake)
{
Console.WriteLine("No shake.");
Console.WriteLine(sentence);
break;
}
// remove left most pattern
sentence = sentence.Remove(firstIndex, pattern.Length);
// remove right most pattern
sentence = sentence.Remove(secondIndex - pattern.Length, pattern.Length);
// remove middle char from pattern
pattern = pattern.Remove(pattern.Length / 2, 1);
Console.WriteLine("Shaked it.");
}
}
// Recursive solution
public static string ShakeRecursive(string sentence, string pattern)
{
var firstIndex = sentence.IndexOf(pattern);
var secondIndex = sentence.LastIndexOf(pattern);
var canShake = firstIndex > -1
&& secondIndex > -1
&& pattern.Length > 0;
if (!canShake)
{
Console.WriteLine("No shake.");
return sentence;
}
// remove left most pattern
sentence = sentence.Remove(firstIndex, pattern.Length);
// remove right most pattern
sentence = sentence.Remove(secondIndex - pattern.Length, pattern.Length);
// remove middle char from pattern
pattern = pattern.Remove(pattern.Length / 2, 1);
Console.WriteLine("Shaked it.");
return ShakeRecursive(sentence, pattern);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment