Category Archives: Strings

View algorithms on Strings

Determine if two strings share a common sub-string.

Question: Given two strings, determine if they share a common sub-string. If they have a common sub-string, print YES else print NO. Input: studyalgorithms algos Output: YES This is one of the easy problems but can get a little tricky, if we do not understand it completely. Let us try to understand the test case String 1 =… Read More »

[Hackerrank] – Palindrome Index

Question: Given a string, identify the index of character to be removed to change the string into a palindrome. If the string cannot be converted to palindrome or is already a palindrome just return -1. Input: abckdeedcba Output: 3 (0 based indexing) To start off with the problem, we must first understand that there can be two possible… Read More »


Question: Given a string, identify if it is a pangram. Input: The quick brown fox jumps over the lazy little dog. Output: pangram To start with the problem statement we must first understand what is a pangram. A pangram is a string that contains all the characters in the English alphabet at-least once. This means that the string… Read More »

[Hackerrank] – Two Characters

A string is said to be valid when it has only distinct characters and none of them repeat simultaneously. For example, if string ‘s two distinct characters are x and y, then valid examples could be xyxyx or yxyxy but not xxyy or xyyx. Question: Given a sample string, we need to determine what is the maximum length… Read More »

[Hackerrank] – Camel Case

Question: Given a camelCase string, find the number of words in the string. Input: saveChangesInTheEditor Output: 5 Camel case is a very popular style of naming variables and methods in most of the programming languages. Not only it helps to make the code readable but also ensures that the variable names are unique and easy to remember. A… Read More »

Next lexicographic string permutation

Question: Given a string, find the next lexicographic permutation. Input: abc Output: acb First of all let us try to understand what do you mean by next lexicographic string. To get a hold of the concept try to remember an English dictionary and how words are arranged in it. We start with the letter ‘a’, write all the… Read More »

Common String Algorithms

In this post, I would try to implement the following string-related algorithm questions. I would like to implement these using Java and try to make additional comments which I think that are useful for understanding the implementations of some type of data structures in Java programming language. Non Repeated Characters in String : Return the unique characters in… Read More »

Easiest way to change case of alphabets

Here we will discuss the easiest and most simple way to change case of alphabets. Hint : We will be using bit hacks. Often during our programming needs we need to change the case of alphabets. This can be a problem requirement or simply our need to solve the question. There are several ways to do it, but… Read More »

Longest substring without repeating characters

Question: Given a string, find the length of the longest substring without repeating characters. Input: “abcabcbb” Output: “abc” Input: “bbbb” Output: “b” The longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1. We definitely have the brute-force method, where we find… Read More »