diptivs
BAN USER
bool findIfSubSequesnce(string input)
{
map<char, int> m;
map<char, int>::iterator iter;
int i,j;
int len = strlen(input.c_str());
bool isCharRepeated=false;
/*
Step 1: Scan the string to count the number of each letter.
(mean while if the count of any letter
is 4 or more, we already find a repetition of two character, the same character followed by itself and return YES)
If no character is repeated, return NO.
*/
for (i = 0; i < len; i++)
{
iter = m.find(input[i]);
if (iter != m.end())
{
iter->second++;
isCharRepeated = true;
if (iter->second >= 4)
return true;
}
else
{
m.insert(pair<char, int>(input[i], 1));
}
}
if (!isCharRepeated)
return false;
/*
Step 2: Scan second time and append each letter that has been repeated (using the count array from the first step) to a new string (let say a StringBuilder)
*/
string str = "";
for (i = 0; i < len; i++)
{
iter = m.find(input[i]);
if (iter != m.end())
{
if (iter->second > 1)
str += input[i];
}
}
/*
Step 3: If the new string is not palindrom, return YES.
else if newString.length() is odd and the middle is different from one previous return YES.
return NO.
*/
len = strlen(str.c_str());
for (i = 0, j = len - 1; i < j; i++, j--)
{
if (str[i] != str[j])
return true;
}
if (i == j)
{
if (str[i - 1] != str[i])
return true;
else
return false;
}
else
return false;
}
int main(int argc, char* argv[])
{
bool ans = findIfSubSequesnce("abcdacb");
return 0;
}
struct Node {
int val;
int index;
vector<Node*> children;
};
void createTree(Node *head)
{
Node *node, *qNode;
char ch;
queue<Node *> q;
cout << "Enter value of head node: ";
cin >> head->val;
head->index = 0;
q.push(head);
while (!q.empty())
{
qNode = q.front();
q.pop();
cout << "do you want to add child of " << qNode->val << "?" << endl;
cin >> ch;
while (ch == 'y' || ch == 'Y')
{
node = new Node();
cout << "Enter value of head node: ";
cin >> node->val;
node->index = 0;
qNode->children.push_back(node);
q.push(node);
cout << "do you want to add another child of " << qNode->val << "?" << endl;
cin >> ch;
}
}
}
//Recursive solution
void postorderRec(Node *head)
{
if (head == NULL)
{
cout << "empty tree" << endl;
return;
}
if (head->children.empty())
cout << head->val << " ";
else
{
for (vector<Node *>::iterator iter = head->children.begin(); iter != head->children.end(); iter++)
{
postorderRec(*iter);
}
cout << head->val << " ";
}
}
//Iterative solution
void postorderiTer(Node *node)
{
if (node == NULL)
{
cout << "empty tree" << endl;
return;
}
stack <Node *>st;
Node *temp;
st.push(node);
while (!st.empty())
{
temp = st.top();
if (temp->index < temp->children.size())
{
node = temp->children[temp->index];
temp->index++;
st.push(node);
}
else
{
st.pop();
cout << temp->val << " ";
}
}
}
int main()
{
Node *head = new Node();
createTree(head);
postorderRec(head);
cout << endl;
postorderiTer(head);
return 0;
}
struct Node {
int val;
vector<Node*> children;
};
void createTree(Node *head)
{
Node *node, *qNode;
char ch;
queue<Node *> q;
cout << "Enter value of head node: ";
cin >> head->val;
q.push(head);
while (!q.empty())
{
qNode = q.front();
q.pop();
cout << "do you want to add child of " << qNode->val << "?" << endl;
cin >> ch;
while (ch == 'y' || ch == 'Y')
{
node = new Node();
cout << "Enter value of head node: ";
cin >> node->val;
qNode->children.push_back(node);
q.push(node);
cout << "do you want to add another child of " << qNode->val << "?" << endl;
cin >> ch;
}
}
}
//Recursive solution
void postorderRec(Node *head)
{
if (head == NULL)
{
cout << "empty tree" << endl;
return;
}
if (head->children.empty())
cout << head->val << " ";
else
{
for (vector<Node *>::iterator iter = head->children.begin(); iter != head->children.end(); iter++)
{
postorderRec(*iter);
}
cout << head->val << " ";
}
}
//Iterative solution
void postorderiTer(Node *node)
{
if (node == NULL)
{
cout << "empty tree" << endl;
return;
}
stack <Node *>st;
map<Node *, int> m;
Node *temp;
int index;
st.push(node);
m.insert(pair<Node *,int>(node, 0));
while (!st.empty())
{
temp = st.top();
index = m.find(temp)->second;
if (index < temp->children.size())
{
m.find(temp)->second++;
node = temp->children[index];
st.push(node);
m.insert(pair<Node *, int>(node, 0));
}
else
{
st.pop();
cout << temp->val << " ";
}
}
}
int main()
{
Node *head = new Node();
createTree(head);
postorderRec(head);
cout << endl;
postorderiTer(head);
return 0;
}
Repnickjonson885, Consultant at ASU
I am Department director in a Libera company. I live in Buffalo NY, USA. I am a hard worker girl ...
Repjesusitahyer, Data Engineer at ASAPInfosystemsPvtLtd
Hello Everyone, I am Jesusita and I am passionate about writing the stories about powerful mantra to get what you ...
Repjesselblagg9, Cloud Support Associate at 247quickbookshelp
Hello, I am Jesse and I live in Springfield, USA. I am working in a company as an engineering technician ...
Repbarrietrosado, abc at ABC TECH SUPPORT
Hi, I am Barrie, from Ualapue USA. I believe that every challenge has a solution - and I take great satisfaction ...
Reppaulinedsisk, Testing / Quality Assurance at Cloudmere, Inc.
I want to become a successful professional in a highly competitive technological world where performance is rewarded with exciting new ...
RepSherriMooney, Network Engineer at Arista Networks
I am not a model but as a photographer, I can't see how one could call it fun. Matter ...
RepShirleyMHansen, Project Leader at Chelsio Communications
I am Physical Therapy Aide with 3 years experience in hospital. I like to build up my knowledge of various ...
Repellenabeaudry4, Analyst at Boomerang Commerce
I'm a 23 year-old blogger, make-up junkie and follower of Hinduism.I love Reading because it brings happiness for ...
Repcoragkemmer, Data Scientist at Bankbazaar
Have years of experience to treating variety of outpatients with modalities such as massage, exercise, paraffin, joint mobilization, mechanical traction ...
Repsusanaminick, Research Scientist at CapitalIQ
I am Susan from Dallas, I am working as a Property manager in Kohl's Food Stores company. I love ...
Repwaynebgrover, AT&T Customer service email at ASAPInfosystemsPvtLtd
I am 31 years old and live in San Jose with my family. I have all types of books and ...
Repdorarwoods, Kuwait airways reservations at Aspire Systems
I am an Application engineer in the network chef company. In my free time, I enjoy reading programming and technology ...
RepPrankHwa, Animator at Accolite software
I am a Reporter and responsible for delivering updates and analysis on current events with the main goal to keep ...
RepI believe in magic, power, aliens, parallel universe, god. I always dream about the powers and out of the world ...
Repmonamore609, Android test engineer at Cisco Systems
Data entry clerks are responsible for inputting a high volume of data from multiple sources into a database, ensuring that ...
- diptivs December 20, 2015