String Basics

A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation).

Some languages provide strings as a builtin datatype ( Like C++ , Java , C# ) whereas some implements string as an array of characters ( Like C ).

Strings are not available in C instead, we use a char array to read strings, where the end of string is marked with the special character \0 often called as null character.

When we have a char arr[] in C and want to iterate over the characters with a loop like

for (int i = 0;i < strlen(arr);i++){ 
    printf("%c",arr[i]) ;
}    

we have to be careful because by using strlen(arr), the complexity of the operation goes unknowingly up to . That is because strlen(arr) is a operation in itself, so it should not be used in the termination condition.

int len = strlen(arr) ;
for (int i = 0;i < len;i++){ 
    printf("%c",arr[i]) ;
}

Substring:

A substring is a part of string such that . It is a contiguous slice of the original string.

For example : List of substrings of string S = "abc" contains following strings.

  1. a
  2. b
  3. c
  4. ab
  5. bc
  6. abc

Therefore, a string of length contains substrings.

Subsequence:

A subsequence is a sequence that can be derived from another sequence by deleting some elements ( possibly zero but not all ) without changing the order of the remaining elements.

For example : List of subsequences of string S = "abc" contains following sequences.

  1. a
  2. b
  3. c
  4. ab
  5. bc
  6. ac
  7. abc

Therefore, a sequence of size contains subsequences.

Subset:

Subset is any unordered set of elements from the original list.

For example : List of subsets of string S = "abc" contains following sets.

  1. {}
  2. {a}
  3. {b}
  4. {c}
  5. {c,b}
  6. {a,b}
  7. {a,c}
  8. {a,b,c}

Therefore, a set of size contains subsets.

NOTE :

  1. {b,a,c} is a subset of string "abc" but not a subsequence.
  2. Each subsequence of a collection of elements is its subset also, but reverse does not hold.

Sublist:

Sublist is any unordered list derived from the original list. Here elements need not be unique, but should exist on separate indices in the original list.

 
Related challenge for String Basics
Go to Top

Greedy Technique

A greedy algorithm is an algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum.

A very common problem which gives good insight into this is the Job Scheduling Problem.

You have jobs numbered and you have the start times and the end times for the job. Which jobs should you choose such that you get the maximal set of non-overlapping jobs?

The correct solution to this problem is to sort all the jobs on the basis of their end times and then keep on selecting the job with the minimal index in this list which does not overlap with currently selected jobs.

Sounds intuitive, but why does it work?

Well, since each job has equal weight, selecting the one which makes way for newer jobs sooner is optimal. Although a more formal argument can be made for a rigorous proof, the intuition behind it is similar.

Now, let's consider another problem. You again have jobs. Each job can be completed at any time and takes time to complete and has a point value of . But with each second, the point value of the job decreases by . If you have to complete all the jobs, what is the maximum points that you can get?

The problem basically asks us for an order of accomplishing the jobs.

Here, doing the job with higher first makes sense. At the same time, doing the job with lower also sounds good. So how do we decide?

Assume you have just jobs which, without loss of generality, can be numbered as and .

Now, if you do job before job , your net score is:


Otherwise,

If then:
In other words, it is optimal to do job before job iff .

Notice that this argument can be applied to jobs as a sorting rule. The job with maximum value should be done first and so on.

This gives us the optimal ordering and is also in line with our intuition.

Greedy doesn't always work

Greedy solutions are usually good whenever they exist, because they are usually easy to implement and usually have a fast running time. However, greedy algorithms don't always work! By this, we don't mean that the greedy algorithm fails to return the correct answer on all inputs. Instead, we mean that the algorithm fails on at least one input.

For example, consider the following problem: You again have jobs, and the job takes time to complete and has a point value of . This time, the point values do not decrease over time, and you don't have to finish all jobs. Unfortunately you only have a total of time to spend. What is the maximum points you can get?

One greedy algorithm that comes to mind is the following: while there is still time remaining, take the job with the largest point value that can be finished within the remaining time. Intuitively, this can be seen to work in some cases. However, this fails in the following set of jobs:

Assuming , the greedy algorithm mentioned above first takes job , then job , for a total of points. However, this is not the global optimum, because you can take jobs and for a total of points, which is much higher.

The next greedy algorithm we can try is to always take the job which takes the shortest amount of time to finish. However, this also fails the set of jobs above (where you only get points).

You can probably try crafting a more sophisticated greedy algorithm than the ones we described, but it probably won't work, i.e. it will probably fail on some input. This is because the problem we described to you is equivalent to the knapsack problem which currently has no known efficient algorithm!

 
Related challenge for Greedy Technique
Go to Top
  1. Challenge Walkthrough
    Let's walk through this sample challenge and explore the features of the code editor.1 of 6
  2. Review the problem statement
    Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
  3. Choose a language
    Select the language you wish to use to solve this challenge.3 of 6
  4. Enter your code
    Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
  5. Test your code
    You can compile your code and test it for errors and accuracy before submitting.5 of 6
  6. Submit to see results
    When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
  1. Check your score