Repeated String

A day or so ago I read a post on the Financial Times titled “How the Modern Office is Killing Our Creativity”. This is nothing new but seems that most people do not wish to understand the consequences and apply changes to improve. This specially holds for the CEO down to the project managers.

Since I can remember, I have always tried to set a reminder of what I need to achieve in a specified time period. In some cases I write down the set of tasks. In others I just repeat a set of memorized steps. One way or the other, if possible I perform the tasks in solitude. Of course there are some tasks that need to be performed with others (e.g., consultation regarding an issue, ideas if I get stuck, help team members when they need it).

I have read “Work Deep” by Cal Newport. In his book it provides the reader with examples of successful people and what they did in order to generate their best work. Trust me, it was not reading and answer social media, emails, phone calls. They achieved success by concentrating every day on the tasks they deemed important.

Last week I had the opportunity of visiting a Fortune 100 company. I have to admit that I was there around lunch time. People were having lunch and working. Cubicles and open area. No place to sit down in peace to think and get things done. It gave me the impression that I was visiting an open market in Asia or the Middle East.

I was talking with a person in sales in a different Fortune 100 company. She mentioned that they work in an open environment with low wall cubicles. One can hear what is going on all over (e.g., people eating, talking, on the phone, bouncing balls). She finds it hard to get real work done. Thankfully there is a set of four or six small open offices in one floor of the 10 story building where she works. More than once when she is working in one of the offices, a coworker has topped by to see if something is wrong with her. She always responds that she needs to get some work done.

On different occasions I have spent some time in Redmond at Microsoft. Unless something has changed in the past few years, developers and product managers have their own office. They can close the door and work in silence if they wish. I assume that people that needs to think at work needs to be left alone. Of course, there are technical and managerial meetings. We cannot work in a vacuum.

My opinion is that in the past four or so decades, the pendulum has moved on the opposite direction. This is one of the main reasons software is getting so big and buggy. A huge percentage of development and support budget is fixing bugs. Refactoring is just the nice way to say “the software is not good, we need to rewrite it”. Perhaps the initial architecture and design were fine, but after so many changes to enhance and fix bugs, development teams need to spend quality time thinking on how to implement and address the technical debt.

Sorry, I went too far on a tangent. Like to keep the introductions short on each post, but I really believe it is time to think how software systems and services are developed and maintain during their SDLC. Things seem to be getting expensive and out of control.

The subject of this post is quite simple and short. It is based on the Repeated String HackerRank challenge. I believe the requirements are well expressed.

The idea is to find how many instances of letter ‘a’ are in the first n characters of a string generated by concatenating the provided string an infinite number of times. Of course the argument n limits the size of infinity!

Allow me to provide an edited screen capture from my Eclipse IDE with some sample cases I used:











Take for example the first case. We are provided with the string “aba” and we need to append enough instances to get a string of 10 characters. In this case the generated string would be “abaabaabaa”. Our task is to return the count of letter ‘a’ which in this case would be 7.

The function for the solution follows:

    // Complete the repeatedString function below.
    static long repeatedString(String s, long n) {

    	System.out.println("s ==>" + s + "<== s.length: " + s.length() + " n: " + n);
    	// **** determine how many times a is found in the specified string ****
    	long numOfAs	= 0;
    	for (int i = 0; i < s.length(); i++) {
    		if (s.charAt(i) == 'a')
    	System.out.println("numOfAs: " + numOfAs);
    	// **** determine the initial count of letter 'a' ****
    	long countOfAs = n / s.length() * numOfAs;
    	System.out.println("countOfAs: " + countOfAs);
    	// **** take into account the instances of 'a' in the rest of the string ****
    	long mod = n % s.length();
    	System.out.println("mod: " + mod);

    	// **** count additional 'a' character(s) ****
    	for (int i = 0; i < mod; i++) {
    		if (s.charAt(i) == 'a')
    	System.out.println("countOfAs: " + countOfAs);
    	// **** return the total count of letter 'a' ****
    	return countOfAs;

We could always generate the string and count the instances of the letter in question. That would be considered the brute force approach. I did not try it but if we would it would probably time out.

Instead let’s reason what is going on. There are some pieces of information we might use i.e., length of the base string, the count of letter ‘a’ in the base string, and the size of the final string. We can then compute the initial number of instances of letter ‘a’ in the long string up to the multiple sizes. We can then add to it the number of remaining letter ‘a’ instances in order to complete the specified length; in our sample case 10.

The entire solution written in Java may be found in my GitHub repository.

Hope you enjoyed this post. I believe the HackerRank challenges help me explore situations that I do not encounter on my daily work.

If you have comments or questions regarding this post or any other one in this blog, or if you need help in any or all of the SDLC of a project, please leave me a note bellow. I will not make public requests for help.

Keep on reading and experimenting. It is the best way to learn.


Follow me on Twitter:  @john_canessa


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.