Category Archives: String

Check if a string is a double string

Given a string, we need to check if a string is a double string possibly by deleting a character.

A double string is a repetition of two strings affixed together.
For example the following strings are double strings
"aa", "meme", "abbabb"
Where as the follwing are not double strings
"ab", "abc", "acbab"

However "acbab" can be be a double string if we remove "c" from it.

This problem was appeared in Codechef March 2016 long contest. Here is the link.

Let us try to analyze the problem. Consider the base case first. A string of length 0 or 1 can never be a double string. Hence, we have to consider the strings of length > 1. Also we can observe that a double string is always of even length.

Case 1:
If the given string is of even length, we simply need to check if it is a double string by checking if the left half and right half are equal.
No need to consider the case of deleting a character because, if we delete a character, it’s length will be odd which will never be a double string.

Case 2:
If the string is of odd length, we have to delete a character before checking if it is a double string. How to find out which character to be deleted?

Consider the following cases
"cabab", delete first character
"acbab", delete second character
"abcab", delete third character
"abacb", delete fourth character
"ababc", delete fifth character

So it appears that we should try to remove each possible character and check if it is a double string. However since we want to check for a double string,
the left and right half should atleast be similar if not equal.

Consider an example “acbab”, we can divide the string into longer and shorter halves in two ways
("acb", "ab")
("ac", "bab")

Since the first pair of string differ by only character (In other words, their edit distance is just 1)

So we just need to check if the edit distance between the longer and shorter strings is at most 1.

Here is the C++ implementation of the above algorithm. It runs in O(n) time.

Counting characters in the given string using C++ STL map

Map is one of the most useful data structure in solving many programming problems. Today we will see how to use C++ STL map with a simple example. Counting the frequency of letters in a given string. The map data structure stores <key,value> pairs. In this example key will be the character, and the value will be it’s frequency. Here is the code to do this.

#include <iostream>
#include <string>
#include <map>

using namespace std;

void getFrequency(string strInput,map<char,int> & fMap)
{
map<char,int>::iterator it; //iterator to find the entries in map
for(int i = 0 ; i < strInput.length() ; i++ )
{
char ch = strInput.at(i);
it = fMap.find(ch); //find the character in the map
if( it == fMap.end() ) //if not present in the map
{
fMap.insert( make_pair(ch,1) );//add an entry
}
else
{
it->second++; //else increment the frequency
}
}
}

void printFrequency(map<char,int> & fMap)
{
map<char,int>::iterator it;//iterator to loop through all the chars
for( it = fMap.begin() ; it != fMap.end() ; ++it )
{
cout<<"["<< it->first<<"]"<<"->"<<it->second<<endl;
}
}

int main()
{
string strInput;
//read input string; this implementation does not reed space separated strings
cin>>strInput;
map<char,int> frequencyMap; //frequency map
getFrequency(strInput, frequencyMap); //get the frequencies
printFrequency(frequencyMap); //print the frequencies
return 0;
}