deepthi.shetty90
BAN USERHey, I have worked around this problem to some extent and below is the code.
I am reading the strings from the files.
comment in your questions with respect to the code.
#include <iostream>
#include <list>
#include <map>
#include<algorithm>
#include<sstream>
#include<fstream>
using namespace std;
string Hash(string);
int main()
{
list<string> words;
map<string,int> dictionary;
//Hash("candy");
//dictionary has values of hash strings assigned to 0;
dictionary["c3y"] =0;
dictionary["d3y"] = 0;
dictionary["a5y"] = 0;
ifstream inputfile;
inputfile.open("inputStringList.txt");
string str;
while(getline(inputfile,str))
{
string hashvalue = Hash(str);
if(dictionary.find(hashvalue) != dictionary.end()){
cout<<str<<" is not a unique string"<<endl;
dictionary.find(hashvalue)->second++;
}else{
cout<<str<<" is a unique string"<<endl;
dictionary[hashvalue] =0;
}
}
inputfile.close();
return 0;
}
string Hash(string str)
{
int len = str.length();
int hashLen = len-2;
string hashValue ="";
hashValue+= str[0];
ostringstream convert;
convert<<hashLen;
hashValue+=convert.str();
hashValue+=str[len-1];
return hashValue;
}
Below is the code that I have written in C++,
Comment if you think code can be made more efficient
#include <iostream>
#include <list>
#include <map>
#include<string>
#include <utility>
using namespace std;
void findsubstring(string,list<string>,map<string,int>&);
int main()
{
list <string> l;
l.push_back("aca");
l.push_back("cat");
l.push_back("hello");
l.push_back("world");
string input = "acacabcatghhellomvnsdb";
map<string,int> _map;
list<string>::iterator li = l.begin();
while(li != l.end())
{
// _map.insert(*li,0);
_map[*li] = 0;
li++;
}
map<string,int>::iterator mitr = _map.begin();
while(mitr != _map.end())
{
cout<<mitr->first<<" "<<mitr->second<<endl;
mitr++;
}
findsubstring(input,l,_map);
mitr = _map.begin();
while(mitr != _map.end())
{
cout<<mitr->first<<" "<<mitr->second<<endl;
mitr++;
}
return 0;
}
void findsubstring(string inputS,list<string> l,map<string,int>& m)
{
//int len = input.length();
list<string>::iterator ltr = l.begin();
while(ltr != l.end())
{
string input = inputS;
int step;
for(int i=0; i<input.length()-1; i++)
{ step = input.find(*ltr);
if(step!= string::npos)
{
step++;
m.find(*ltr)->second++;
// string st = *ltr;
// step = step+st.length();
}
else{
step =i+1;
}
input = input.substr(step,input.length());
}
ltr++;
}
}
#include <iostream>
- deepthi.shetty90 April 19, 2016#include <list>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
void FindAnagram(string,string,map<string,int>&);
bool map_compare(const map<char,int>m1,const map<char,int>m2){
return (m1.size() == m2.size() && equal(m1.begin(),m1.end(),m2.begin()));
}
int main()
{
list<string> lists;
lists.push_back("tea");
lists.push_back("ate");
lists.push_back("eat");
lists.push_back("apple");
lists.push_back("java");
lists.push_back("vaja");
lists.push_back("cut");
lists.push_back("utc");
string input [] = {"tea","ate","eat","apple","java","vaja","cut","utc"};
map<string,int> mapString;
int len = (sizeof(input)/sizeof(string));
cout<<len;
for(int i=0;i<len-1;i++){
for(int j=i+1;j<len;j++){
FindAnagram(input[i],input[j],mapString);
}
}
//display output
map<string,int>::iterator mtr = mapString.begin();
while(mtr != mapString.end()){
cout<<mtr->first<<endl;
mtr++;
}
return 0;
}
void FindAnagram(string str1,string str2,map<string,int>& output){
//check if the size of the strings are same
if(str1.length() != str2.length())
return;
int charMatch[256]={0};
for(int i=0;i<str1.length();i++){
int asci = (int)str1[i];
charMatch[asci]++;
asci = (int) str2[i];
charMatch[asci]--;
}
//make sure the charMatch has all entry zero
bool flag = true;
for(int i=0;i<256;i++){
if(charMatch[i] !=0){
flag = false;
}
}
if(flag == true){
output[str1]++;
output[str2]++;
cout<<"Match found";
}
//shorter version to check the strings:
string s1 = str1;
string s2 = str2;
sort(s1.begin(),s1.end());
sort(s2.begin(),s2.end());
if(s1.compare(s2) ==0){
cout<<"matching"<<s1<<" and "<<s2<<endl;
}
}