Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring without repeating characters.

For example –

Example 1:

Input: “abcabcbb” Output: 3

The output string is “abc”, with a length of 3.

Example 2:

Input: “bbbbb” Output: 1

The longest substring in this example is “b”. Its length is 1.

Example 3:

Input: “pwwkew” Output: 3

The answer is “wke”. Its length is 3.

Sort Characters by Frequency

In this tutorial, I am going to discuss a very interesting problem sort characters by frequency.

Given a string, write a method to sort it in decreasing order based on the frequency of characters.

For example:

Example 1

Input : “teee”

Output: “eeet”

Example 2:

  Input: “dddbbb”

Output: “dddbbb”   or “bbbddd” (Both d and b appear 3 times so any of the above output are valid)

  Example 3:

  Input:  “Eett”

Output: “ttEe” or “tteE”  (Both e and E are two different characters so the output should not be “Eett”)

Find Peak Element in Array

Given an array of integers, write a code to find peak element in array. The array may contain multiple peak elements, in that case, return anyone peak element.

Peak Element

Peak element is an element which greater than it’s neighbours. For strictly decreasing array, the first element is the peak element. And for strictly increasing array the last element is the peak element.

For example:

Example 1:

Input: {2, 3, 4, 7, 5} , Output: 7

The element 7 is greater than it’s neighbours (4 and 5).

Example 2:

Input: {8, 7, 6, 5, 4}, Output: 8

In this example, An array is strictly decreasing, so the first element is the peak element.

Example 3:

Input: {2, 3, 4, 5, 6}, Output: 6

An array is strictly increasing, so the last element is the peak element.

Example 4:

Input: {2, 2, 2, 2, 2}, Output: 2

All the array element is the same, So every element in this array is the peak element.