Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created April 3, 2016 18:59
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/07fe563e1d8e65dc3361d8d30eefe347 to your computer and use it in GitHub Desktop.
Save jianminchen/07fe563e1d8e65dc3361d8d30eefe347 to your computer and use it in GitHub Desktop.
Reverse Shuffle Merge - still only one case pass - after 2 hours working
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReverseMergeString
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(reverseMerge(Console.ReadLine()));
}
/*
* Failed test case:
* bdabaceadaedaaaeaecdeadababdbeaeeacacaba
* output:
* aaaaaabaaceededecbdb
* Julia's output "cdeadababdbeaeeacacaba" string
*/
public static string reverseMerge(string s)
{
if (!canBeReverseMerged(s))
return "";
int len = s.Length;
int half = len / 2;
int[] A = getCountArray(s);
int[] skipLog = getHalf(A);
String output = "";
for (int i = 0; i < s.Length; i++ )
{
char c = s[i];
if (firstOneNonzero(skipLog, c) || !greedyCheckBackwardSkip(skipLog, c))
output += c;
else
{
if (!canDecrement(skipLog, c))
return ""; // something is wrong
}
}
return output;
}
private static bool canDecrement(int[] A, char c)
{
A[c-'a']--;
return (A[c - 'a'] >= 0);
}
/*
* if c can be added to the output
*/
private static bool greedyCheckForward(int[] A, char c)
{
for(int i=0; i<'c'-'a';i++)
{
if (A[i] > 0)
return false;
}
return true;
}
/*
* Skip rules - only two :
* 1. First, if there is one to skip before the input char
* 2. Second, if the char is the first one in the list, alway go to true
*/
private static bool greedyCheckBackwardSkip(int[] A, char c)
{
for (int i = 25; i > 'c' - 'a'; i--) // bug: index problem - why I am still need debugger here!!!
{
if (A[i] > 0 && A[i] != (c - 'a'))
return true;
}
return false;
}
// c is first char nonzeor
private static bool firstOneNonzero(int[] A, char c)
{
for (int i = 25; i > 'c' - 'a'; i--) // bug: index problem - why I am still need debugger here!!!
{
if (A[i] > 0 )
return false;
}
return true;
}
private static int[] getHalf(int[] A)
{
int[] ret = new int[26];
for(int i=0; i<26; i++)
{
ret[i] = A[i] / 2;
}
return ret;
}
private static bool isHalf(int[] B, int[] A)
{
for(int i=0;i<26;i++)
{
if (B[i] * 2 == A[i])
continue;
else
return false;
}
return true;
}
// assume both string have same length
// because it is reverse string
private static string smallOne(string tmpS1, string tmpS2)
{
string s1 = reverse(tmpS1);
string s2 = reverse(tmpS2);
int len = s1.Length;
for(int i=0;i<len;i++)
{
if (s1[i] < s2[i])
return tmpS1;
else if (s1[i] > s2[i])
return tmpS2;
}
// both are the same, any one
return tmpS1;
}
private static string reverse(string s)
{
StringBuilder newS = new StringBuilder();
for (int i = s.Length - 1; i >= 0; i--)
newS.Append(s[i].ToString());
return newS.ToString();
}
private static bool canBeReverseMerged(string s)
{
if (s == null || s.Length == 0) return true;
int[] A = new int[26];
for(int i=0; i < s.Length;i++)
{
A[s[i]-'a']++;
}
for(int i=0;i<26;i++)
{
if (A[i] % 2 == 1)
return false;
}
return true;
}
private static int[] getCountArray(string s)
{
int[] A = new int[26];
if (s == null || s.Length == 0) return A;
for (int i = 0; i < s.Length; i++)
{
A[s[i] - 'a']++;
}
return A;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment