Comparing two strings to see if they are rotation [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Rotation means one string is created from moving the other string one or more bits to the right. For example abc and cab are rotation, abcd and bacd are not rotation.
I wrote the code below however it's failed to pass the last one test case (don't know what it is). Could anyone give me some hints about where goes wrong or is there any more efficient algorithm:
int isLetterInWord(char c, char* word)//find first letter in the word which is equal to c
{
int len = strlen(word);
for(int i=0; i<len; ++i)
{
if(c==word[i])
return i;
}
return -1;
}
int isRotation(char* word1, char* word2)
//check if word1 and word2 are rotation. if so return 1 otherwise -1
{
if(word1 == NULL && word2 == NULL)
return 1;
int len1 = strlen(word1);
int len2 = strlen(word2);
if(len1!= len2)
return -1;
for(int i=0; i<len1; ++i)
{
int pos = isLetterInWord(word1[i], word2);
if(pos == -1)
return -1;
else
{
int p1 = i, p2 = pos;
int cnt=0;
while(cnt<len1)
{
if(word1[p1++]!=word2[p2++])
break;
if(p1==len1)p1=0;
if(p2==len2)p2=0;
cnt++
}
if(cnt==len1)
return 1;
}
}
return -1;
}

Another algorithm of solving this question is as follow:
Lets say first string is str1 and need to check whether str2 is rotation of str1.
The algorithm is as follow:
concatanate str1 to str1. Lets call it str3.
Now check whether str2 is a sub-string of str3.
If str2 is sub-string of str3 then it is a rotation of str1 otherwise not.
Please find the function to check rotation of a string:
int Rotations(char *str1, char *str2)
{
int size1 = strlen(str1);
int size2 = strlen(str2);
char *temp;
void *ptr;
if (size1 != size2)
return 0;
temp = (char *)malloc(sizeof(char)*(size1*2 + 1));
temp[0] = '\0';
strcat(temp, str1);
strcat(temp, str1);
ptr = strstr(temp, str2);
free(temp);
if (ptr != NULL)
return 1;
else
return 0;
}

if(word1==NULL&&word2==NULL)
This should be
if(word1==NULL||word2==NULL)
Not gone through your logic.

Overview: Find first char matching char in string, Then perform xor on string, Till end of minimum length string, This way you can find whether string are rotation string of each other
Pre-Condition: Length of both strings are equal, As already stated above answers
Step 1: Find first matching char in both string notice pos(position) of char in the first string.
Step 2: Now perform xor on first string from pos to the end of first string, With substring with same length from the second string.
Step 3: If result xor is 0 means First part matches of the string matched, And proceed to match remaining part(substring), Which is first char in the first string to char at pos-1, And substring from second string from pos+1 to end of string, Then perform same xor on them,
Step 4: If that matches then exit with sucess, And if does not match then find next matching char in first string, and repeat from step 2, until reaches end of the first string

Related

Last consonant of a text [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
After a user enters a text (with spaces), how do I delete only the last consonant of that certain text?
I have no idea how to do that, whatever I did by now deleted all the consonants.
Have you tried something or you have no idea how to do it?
I'll assume you didn't do anything yet.
First of all, you have to find the last consonant. How do you do that. Well, you start from the end and check the letter until you find a consonant.
Now, when you find the last consonant, you memorize the position of the consonant and then move every letter that's to the right of the consonant to the left.
And lastly, change the last character in the string to the '\0'.
Here is the code:
#include <iostream>
#include <string>
#include <cctype>
bool isConsonant(char);
int main (int argc, char *argv[]) {
char s[80];
// Input the text
std::cout << "Enter the text: ";
std::cin.getline(s,80,'\n');
// Creating string object
std::string text(s);
int length = text.length();
// Searching for the last consonant
int i = length - 1;
while ((!isConsonant(text.at(i)) && (i)))
i--;
// Moving every letter after the last consonant one place to the left
for (int j = i; j < length - 1; j++)
text.at(j) = text.at(j+1);
text.at(length-1) = '\0';
// Printing the text to the standard output
std::cout << text;
return 0;
}
bool isConsonant(char c) {
if (isalpha(c)) {
char temp = c;
if (isupper(c))
temp = tolower(c);
return !((temp == 'a') || (temp == 'e') || (temp == 'i') || (temp == 'o') || (temp == 'u'));
}
return false;
}
Edit: Of course there are other ways to do this, and you should try to think of them. This is a first thing that came to my mind.

Cant find same letters in word [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
can somebody give some explanation how to find same letters in word?
There is the code, but its returning whole word as same letters
int Sting::SameLetters(string word, string sk) {
string find;
find = word;
for (int i = 0; i < word.length(); i++)
{
if ((find.find(tolower(word[i])) == string::npos) &&
(sk.find(word[i]) == string::npos))
{
sk += word[i];
}
}
return sk.length();
}
So i tried to remake code and now its counting letters, but not all
int letters(string word, string sk)
{
sk = word[0];
int lenght = 0;
for ( int i =0; i <= word.length(); i++)
{
if(tolower(sk[0]) == tolower(word[i]))
{
lenght++;
}
sk = word[i];
}
return lenght;
}
My used words for test
ssabba ccea
Results
ssabba(3) ccea(2)
I have to get ssabba(6)
Think about the logic of that if statement for a while.
You initialize find to be the same as word, so when you do
find.find(tolower(word[i])) == string::npos
it will indeed find the character, and the expression will be false. And because that expression is false, due to the short-circuit nature of the logical and operator && the second check will not happen. This means that the sk += word[i] statement will never happen, and you will return the length of the unmodified sk.
I'm not exactly sure what you try to do, but that condition most definitely is not doing what you think it does.
Please have a look at the std::string::find reference. This function will search for the first occurrence in the whole string. You're iterating about all characters, so probably you just want to compare character by character to estimate the count of occurrences from a specific character? Assuming your parameter sk is a std::string with a .length() from exactly 1, you could do it like this:
size_t sameLetters(const std::string& word, const std::string& sk) {
size_t length = 0;
for (size_t i = 0; i < word.length(); i++){
if(tolower(word[i]) == tolower(sk[0]))
length++;
}
return length;
}
That would always return the number of occurrences. i.e.
sameLetters("Aaabc", "a"); /* returns 3 */
sameLetters("hello", "l"); /* returns 2 */
sameLetters("some string", "Z"); /* returns 0 */
If you tried to accomplish something different please elaborate.

counting number of frequency of a word in a text [duplicate]

This question already has answers here:
Counting the Frequency of Specific Words in Text File
(4 answers)
Closed 9 years ago.
I wrote a function for counting frequency of specific word in a text.This program every time return zero.How can I improve it?
while (fgets(sentence, sizeof sentence, cfPtr))
{
for(j=0;j<total4;j++)
{
frequency[j] = comparision(sentence,&w);
all_frequency+=frequency[j];
}}
.
.
.
int comparision(const char sentence[ ],char *w)
{
int length=0,count=0,l=0,i;
length= strlen(sentence);
l= strlen(w);
while(sentence[i]!= '\n')
if(strncmp(sentence,w,l))
count++;
i++;
return count;
}
I have proofread your code and have commented on coding style and variable names. There
is still a flaw I left with the conditional, which is due to not iterating through the
sentence.
Here is your code marked up:
while(fgets(sentence, sizeof sentence, cfPtr)) {
for(j=0;j<total4;j++){
frequency[j] = comparision(sentence,&w);
all_frequency+=frequency[j];
}
}
// int comparision(const char sentence[ ],char *w) w is a poor variable name in this case.
int comparison(const char sentence[ ], char *word) //word is a better name.
{
//int length=0,count=0,l=0,i;
//Each variable should get its own line.
//Also, i should be initialized and l is redundant.
//Here are properly initialized variables:
int length = 0;
int count = 0;
int i = 0;
//length= strlen(sentence); This is redundant, as you know that the line ends at '\n'
length = strlen(word); //l is replaced with length.
//while(sentence[i]!= '\n')
//The incrementor and the if statement should be stored inside of a block
//(Formal name for curley braces).
while(sentence[i] != '\n'){
if(strncmp(sentence, word, length) == 0) //strncmp returns 0 if equal, so you
count++; //should compare to 0 for equality
i++;
}
return count;
}

How to find string in a string

I somehow need to find the longest string in other string, so if string1 will be "Alibaba" and string2 will be "ba" , the longest string will be "baba". I have the lengths of strings, but what next ?
char* fun(char* a, char& b)
{
int length1=0;
int length2=0;
int longer;
int shorter;
char end='\0';
while(a[i] != tmp)
{
i++;
length1++;
}
int i=0;
while(b[i] != tmp)
{
i++;
length++;
}
if(dlug1 > dlug2){
longer = length1;
shorter = length2;
}
else{
longer = length2;
shorter = length1;
}
//logics here
}
int main()
{
char name1[] = "Alibaba";
char name2[] = "ba";
char &oname = *name2;
cout << fun(name1, oname) << endl;
system("PAUSE");
return 0;
}
Wow lots of bad answers to this question. Here's what your code should do:
Find the first instance of "ba" using the standard string searching functions.
In a loop look past this "ba" to see how many of the next N characters are also "ba".
If this sequence is longer than the previously recorded longest sequence, save its length and position.
Find the next instance of "ba" after the last one.
Here's the code (not tested):
string FindLongestRepeatedSubstring(string longString, string shortString)
{
// The number of repetitions in our longest string.
int maxRepetitions = 0;
int n = shortString.length(); // For brevity.
// Where we are currently looking.
int pos = 0;
while ((pos = longString.find(shortString, pos)) != string::npos)
{
// Ok we found the start of a repeated substring. See how many repetitions there are.
int repetitions = 1;
// This is a little bit complicated.
// First go past the "ba" we have already found (pos += n)
// Then see if there is still enough space in the string for there to be another "ba"
// Finally see if it *is* "ba"
for (pos += n; pos+n < longString.length() && longString.substr(pos, n) == shortString; pos += n)
++repetitions;
// See if this sequence is longer than our previous best.
if (repetitions > maxRepetitions)
maxRepetitions = repetitions;
}
// Construct the string to return. You really probably want to return its position, or maybe
// just maxRepetitions.
string ret;
while (maxRepetitions--)
ret += shortString;
return ret;
}
What you want should look like this pseudo-code:
i = j = count = max = 0
while (i < length1 && c = name1[i++]) do
if (j < length2 && name2[j] == c) then
j++
else
max = (count > max) ? count : max
count = 0
j = 0
end
if (j == length2) then
count++
j = 0
end
done
max = (count > max) ? count : max
for (i = 0 to max-1 do
print name2
done
The idea is here but I feel that there could be some cases in which this algorithm won't work (cases with complicated overlap that would require going back in name1). You may want to have a look at the Boyer-Moore algorithm and mix the two to have what you want.
The Algorithms Implementation Wikibook has an implementation of what you want in C++.
http://www.cplusplus.com/reference/string/string/find/
Maybe you made it on purpose, but you should use the std::string class and forget archaic things like char* string representation.
It will make you able to use lots of optimized methods, such as string research, etc.
why dont you use strstr function provided by C.
const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );
Locate substring
Returns a pointer to the first occurrence of str2 in str1,
or a null pointer if str2 is not part of str1.
The matching process does not include the terminating null-characters.
use the length's now and create a loop and play with the original string anf find the longest string inside.

How many palindromes can be formed by selections of characters from a string?

I'm posting this on behalf of a friend since I believe this is pretty interesting:
Take the string "abb". By leaving out
any number of letters less than the
length of the string we end up with 7
strings.
a b b ab ab bb abb
Out of these 4 are palindromes.
Similarly for the string
"hihellolookhavealookatthispalindromexxqwertyuiopasdfghjklzxcvbnmmnbvcxzlkjhgfdsapoiuytrewqxxsoundsfamiliardoesit"
(a length 112 string) 2^112 - 1
strings can be formed.
Out of these how many are
palindromes??
Below there is his implementation (in C++, C is fine too though). It's pretty slow with very long words; he wants to know what's the fastest algorithm possible for this (and I'm curious too :D).
#include <iostream>
#include <cstring>
using namespace std;
void find_palindrome(const char* str, const char* max, long& count)
{
for(const char* begin = str; begin < max; begin++) {
count++;
const char* end = strchr(begin + 1, *begin);
while(end != NULL) {
count++;
find_palindrome(begin + 1, end, count);
end = strchr(end + 1, *begin);
}
}
}
int main(int argc, char *argv[])
{
const char* s = "hihellolookhavealookatthis";
long count = 0;
find_palindrome(s, strlen(s) + s, count);
cout << count << endl;
}
First of all, your friend's solution seems to have a bug since strchr can search past max. Even if you fix this, the solution is exponential in time.
For a faster solution, you can use dynamic programming to solve this in O(n^3) time. This will require O(n^2) additional memory. Note that for long strings, even 64-bit ints as I have used here will not be enough to hold the solution.
#define MAX_SIZE 1000
long long numFound[MAX_SIZE][MAX_SIZE]; //intermediate results, indexed by [startPosition][endPosition]
long long countPalindromes(const char *str) {
int len = strlen(str);
for (int startPos=0; startPos<=len; startPos++)
for (int endPos=0; endPos<=len; endPos++)
numFound[startPos][endPos] = 0;
for (int spanSize=1; spanSize<=len; spanSize++) {
for (int startPos=0; startPos<=len-spanSize; startPos++) {
int endPos = startPos + spanSize;
long long count = numFound[startPos+1][endPos]; //if str[startPos] is not in the palindrome, this will be the count
char ch = str[startPos];
//if str[startPos] is in the palindrome, choose a matching character for the palindrome end
for (int searchPos=startPos; searchPos<endPos; searchPos++) {
if (str[searchPos] == ch)
count += 1 + numFound[startPos+1][searchPos];
}
numFound[startPos][endPos] = count;
}
}
return numFound[0][len];
}
Explanation:
The array numFound[startPos][endPos] will hold the number of palindromes contained in the substring with indexes startPos to endPos.
We go over all pairs of indexes (startPos, endPos), starting from short spans and moving to longer ones. For each such pair, there are two options:
The character at str[startPos] is not in the palindrome. In that case, there are numFound[startPos+1][endPos] possible palindromes - a number that we have calculated already.
character at str[startPos] is in the palindrome (at its beginning). We scan through the string to find a matching character to put at the end of the palindrome. For each such character, we use the already-calculated results in numFound to find number of possibilities for the inner palindrome.
EDIT:
Clarification: when I say "number of palindromes contained in a string", this includes non-contiguous substrings. For example, the palindrome "aba" is contained in "abca".
It's possible to reduce memory usage to O(n) by taking advantage of the fact that calculation of numFound[startPos][x] only requires knowledge of numFound[startPos+1][y] for all y. I won't do this here since it complicates the code a bit.
Pregenerating lists of indices containing each letter can make the inner loop faster, but it will still be O(n^3) overall.
I have a way can do it in O(N^2) time and O(1) space, however I think there must be other better ways.
the basic idea was the long palindrome must contain small palindromes, so we only search for the minimal match, which means two kinds of situation: "aa", "aba". If we found either , then expand to see if it's a part of a long palindrome.
int count_palindromic_slices(const string &S) {
int count = 0;
for (int position=0; position<S.length(); position++) {
int offset = 0;
// Check the "aa" situation
while((position-offset>=0) && (position+offset+1)<S.length() && (S.at(position-offset))==(S.at(position+offset+1))) {
count ++;
offset ++;
}
offset = 1; // reset it for the odd length checking
// Check the string for "aba" situation
while((position-offset>=0) && position+offset<S.length() && (S.at(position-offset))==(S.at(position+offset))) {
count ++;
offset ++;
}
}
return count;
}
June 14th, 2012
After some investigation, I believe this is the best way to do it.
faster than the accepted answer.
Is there any mileage in making an initial traversal and building an index of all occurances of each character.
h = { 0, 2, 27}
i = { 1, 30 }
etc.
Now working from the left, h, only possible palidromes are at 3 and 17, does char[0 + 1] == char [3 -1] etc. got a palindrome. does char [0+1] == char [27 -1] no, No further analysis of char[0] needed.
Move on to char[1], only need to example char[30 -1] and inwards.
Then can probably get smart, when you've identified a palindrome running from position x->y, all inner subsets are known palindromes, hence we've dealt with some items, can eliminate those cases from later examination.
My solution using O(n) memory and O(n^2) time, where n is the string length:
palindrome.c:
#include <stdio.h>
#include <string.h>
typedef unsigned long long ull;
ull countPalindromesHelper (const char* str, const size_t len, const size_t begin, const size_t end, const ull count) {
if (begin <= 0 || end >= len) {
return count;
}
const char pred = str [begin - 1];
const char succ = str [end];
if (pred == succ) {
const ull newCount = count == 0 ? 1 : count * 2;
return countPalindromesHelper (str, len, begin - 1, end + 1, newCount);
}
return count;
}
ull countPalindromes (const char* str) {
ull count = 0;
size_t len = strlen (str);
size_t i;
for (i = 0; i < len; ++i) {
count += countPalindromesHelper (str, len, i, i, 0); // even length palindromes
count += countPalindromesHelper (str, len, i, i + 1, 1); // odd length palindromes
}
return count;
}
int main (int argc, char* argv[]) {
if (argc < 2) {
return 0;
}
const char* str = argv [1];
ull count = countPalindromes (str);
printf ("%llu\n", count);
return 0;
}
Usage:
$ gcc palindrome.c -o palindrome
$ ./palindrome myteststring
EDIT: I misread the problem as the contiguous substring version of the problem. Now given that one wants to find the palindrome count for the non-contiguous version, I strongly suspect that one could just use a math equation to solve it given the number of distinct characters and their respective character counts.
Hmmmmm, I think I would count up like this:
Each character is a palindrome on it's own (minus repeated characters).
Each pair of the same character.
Each pair of the same character, with all palindromes sandwiched in the middle that can be made from the string between repeats.
Apply recursively.
Which seems to be what you're doing, although I'm not sure you don't double-count the edge cases with repeated characters.
So, basically, I can't think of a better way.
EDIT:
Thinking some more,
It can be improved with caching, because you sometimes count the palindromes in the same sub-string more than once. So, I suppose this demonstrates that there is definitely a better way.
Here is a program for finding all the possible palindromes in a string written in both Java and C++.
int main()
{
string palindrome;
cout << "Enter a String to check if it is a Palindrome";
cin >> palindrome;
int length = palindrome.length();
cout << "the length of the string is " << length << endl;
int end = length - 1;
int start = 0;
int check=1;
while (end >= start) {
if (palindrome[start] != palindrome[end]) {
cout << "The string is not a palindrome";
check=0;
break;
}
else
{
start++;
end--;
}
}
if(check)
cout << "The string is a Palindrome" << endl;
}
public String[] findPalindromes(String source) {
Set<String> palindromes = new HashSet<String>();
int count = 0;
for(int i=0; i<source.length()-1; i++) {
for(int j= i+1; j<source.length(); j++) {
String palindromeCandidate = new String(source.substring(i, j+1));
if(isPalindrome(palindromeCandidate)) {
palindromes.add(palindromeCandidate);
}
}
}
return palindromes.toArray(new String[palindromes.size()]);
}
private boolean isPalindrome(String source) {
int i =0;
int k = source.length()-1;
for(i=0; i<source.length()/2; i++) {
if(source.charAt(i) != source.charAt(k)) {
return false;
}
k--;
}
return true;
}
I am not sure but you might try whit fourier. This problem remined me on this: O(nlogn) Algorithm - Find three evenly spaced ones within binary string
Just my 2cents