1

So i recently had an interview where one the question was as below. I have been thinking for a while as to how to solve this. Does anyone know? I have found out that is to do with the combination of strings algorithm.

A string consisting of 'x' and 'y' is given. The task is to split it into 3 seperate parts so that each part contains the same number of letters of 'y'. In how many ways can the string be split.

For example:

Input: "xyxyy"

Output: "xy | xy | y", "xyx | y | y"

If anyone can give me some sort of clue on what I need to know to figure this out or, even better, if you show a code explaining how this could be done that will be great!

Thanks

2
  • 1
    First possibility: next segment right after every y; last possibility: next segment right before every y. Then go through everything in between with all combinations. Commented Aug 2, 2020 at 19:29
  • 1
    Well, it depends on the number of xs between the ys. It doesn't seem like a programming question per se though. It might be a better fit for computer science. Commented Aug 2, 2020 at 19:59

1 Answer 1

3

Simple math will do. So you have a string with 3n 'y' (otherwise you can't split the string at all). The string is something like this:

 aybcdy ... eypq...raybcdy ...  eyvw..zybcdy ...  ey
 <--- n 'y'-->       <--- n 'y'-->     <--- n 'y'-->    

Note that chunks with y a fixed but characters between these parts can be added either to one part or to another:

aybcdy ... ey | pq...raybcdy ...  ey # 1st possibility
<--- n 'y'-->          <--- n 'y'-->

aybcdy ... eyp | q...raybcdy ...  ey # 2nd possibility
<--- n 'y'-->          <--- n 'y'-->

aybcdy ... eypq | ...raybcdy ...  ey # 3d possibility
<--- n 'y'-->          <--- n 'y'-->

...

aybcdy ... eypq...ra | ybcdy ...  ey # the last possibility
<--- n 'y'-->          <--- n 'y'-->

So far we have (P + 1) * (Q + 1) possibilities to split the string into 3 chunks where

  • P - characters between N-th and N+1-st 'y'
  • Q - characters between 2N-th and 2N+1-st 'y'

Code:

private static int Solution(string value) {
  if (null == value)
    return 0;

  int N = value.Count(c => c =='y');

  if (N % 3 != 0 || N == 0) // let's return 0 if we don' have 'y's
    return 0;

  N = N / 3;

  int P = 0;
  int Q = 0;   
  int y = 0;

  foreach (char c in value) {
    if (c == 'y')
      y += 1;
    else if (y == N)
      ++P;
    else if (y == 2 * N) 
      ++Q;
  }

  return (P + 1) * (Q + 1);
}

Demo:

Console.Write(Solution("xyxyy")); 

Outcome:

2

If you want to obtain splits:

private static IEnumerable<string[]> SolutionSplits(string value) {
  if (null == value)
    yield break;

  int N = value.Count(c => c == 'y');

  if (N % 3 != 0 || N == 0) // let's return empty if we don' have 'y's
    yield break;

  N = N / 3;

  List<int> P = new List<int>();
  List<int> Q = new List<int>();
  int y = 0;

  for (int i = 0; i < value.Length; ++i) {
    char c = value[i];

    if (c == 'y')
      y += 1;

    if (y == N)
      P.Add(i);
    else if (y == 2 * N)
      Q.Add(i);
  }

  foreach (int p in P)
    foreach (int q in Q)
      yield return new string[] {
        value.Substring(0, p + 1),
        value.Substring(p + 1, q - p),
        value.Substring(q + 1)}; 
}

Demo:

 string source = "xyxyy";
 
 Console.Write(string.Join(Environment.NewLine, SolutionSplits(source)
   .Select(items => string.Join(" | ", items)));

Outcome:

 xy | xy | y
 xyx | y | y 
Sign up to request clarification or add additional context in comments.

1 Comment

Actually if the letter doesn't exist in the string then the answer would be the number of ways you can split the string into 3 parts since each part would have 0 instances of the letter. Presumably you wouldn't include empty strings in that calculation though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.