-3

You are given three positive integers n, a and b. You have to construct a string s of length n consisting of lowercase Latin letters such that each substring of length a has exactly b distinct letters.

Please provide hints on how to solve. Please dont solve the question. Should I use arrays?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
AKASH ROY
  • 13
  • 5
  • I am facing difficulty in construction of string based on constraints. I mean, there may be more than one strings satisfying. So how can I construct? – AKASH ROY Apr 13 '20 at 16:31
  • Could you provide an example input string and its output? – adelriosantiago Apr 13 '20 at 16:32
  • Can you solve the question when n = a? That way you only need one substring. When you have solved that, extend your solution to cover the more general case. – rossum Apr 13 '20 at 16:39
  • Before using arrays you should use pen and paper, first to see if you can find one solution, then to see if you can generalize it, only then you should consider arrays or no arrays – 463035818_is_not_an_ai Apr 13 '20 at 16:41
  • " I mean, there may be more than one strings satisfying." I don't think this is a problem. The task does not mention uniquness or counting the possible solutions, all you have to do is find one string that matches the constraints – 463035818_is_not_an_ai Apr 13 '20 at 16:42
  • @rossum nice trick to solve for a and generalize. Op you don't need arrays to solve this question you would need hash map , sliding window techniques to solve this. Also I feel can be done using dfs but it might be overkill. – Wasim Ahmad Apr 13 '20 at 20:58

1 Answers1

0

No. This is an easy problem, not necessary to use any external memory like-array or others.

You need just to iterate the loop and generate the characters so that you can construct a string following given conditions. As you are generating characters, you can use ASCII values per iteration with loop to print the characters sequentially (not randomly, otherwise you may confuse).


Again no need to think about the occurrence of exact b numbers of characters in each substrings as you are only generating the b number of characters each time.

Hopeful to your understanding


example: (in C++)

for(int i = 0;  i < n; ++i)
 cout<<char('a'+i%b);
Community
  • 1
  • 1