Category Archives: Strings

View algorithms on Strings

CamelCase matching

Question: Given a set of queries and a pattern, we need to determine if some of those queries can be made from the given pattern by adding one or more lower case character. Output a boolean array where array[i] says true, if the query is possible, else false. Input: queries= {“StudyAlgorithms”,”StudyAlgorithmsTest”,”StanAlluring”,”SimulationAlteration”,”StopStayAlive”} pattern = “StAl” Output: {true, false, true,… Read More »

Length of longest palindrome that can be built from a string

Question: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. Input: “abccccdd” Output: 7 In the above example, the longest palindrome in the given string is “dccaccd” whose length is 7. A palindrome consists of letters with equal partners, plus possibly a unique… Read More »

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 »