Leetcode 28 - Implement strStr(): question - c++

I am experiencing a bug in my submissions for Leetcode 28 that has thus far eluded me. My code works for most test cases but I am getting hung up on scenarios such as haystack = "mississippi", needle = "issip".
I have tried debugging and found that the entire haystack string is iterated through and it is returning -1 or not found. The substring length it is finding at each occurrence of 'i' is 4, 1, 1.
int strStr(string haystack, string needle) {
if (needle.empty()) {
return 0;
}
if (haystack.empty() && !needle.empty()) {
return -1;
}
int i = 0, j = 0, ans = 0;
for (i; i < haystack.length(); i++) {
if (haystack[i] == needle[0]) {
j = 0;
ans = i;
for (j; j < needle.length(); j++) {
/*
if (haystack[i++] == needle[j]) {
continue;
}
else {
break;
}
*/
if (haystack[i++] != needle[j]) {
break;
}
}
if (j == needle.length()) {
return ans;
}
}
if (j == needle.length()) {
return ans;
}
}
return -1;
}
Input: "mississippi", "issip"
Output: -1 (ans = 10, j = 1)

The function has several drawbacks.
For starters it should be declared like
std::string::size_type strStr( const std::string &haystack, const std::string &needle );
and if the second string is not found in the first string the function should return std::string::npos as all similar member functions of the class std::string do.
The function parameters shell be of constant referenced types.
The condition in this if-statement
if (haystack.empty() && !needle.empty())
has a redundant operand. It could be rewritten like
if (haystack.empty())
This loop
for (i; i < haystack.length(); i++)
should stop its iterations when the size of the tail of the first string is less than the size of the second string.
in this if-statement
if (haystack[i++] != needle[j]) {
the variable i is incremented that results in incrementing the variable two times: one in this statement and the second time in the loop.
The second pair of these statements
if (j == needle.length()) {
return ans;
is redundant.
The function can be written the following way as it is shown in the demonstrative program.
#include <iostream>
#include <string>
std::string::size_type strStr( const std::string &haystack, const std::string &needle )
{
if ( needle.empty() )
{
return 0;
}
else if ( haystack.empty() )
{
return -std::string::npos;
}
else
{
std::string::size_type ans = std::string::npos;
auto n1 = haystack.length();
auto n2 = needle.length();
for ( std::string::size_type i = 0; ans == std::string::npos && i + n2 <= n1; i++ )
{
std::string::size_type j = 0;
while ( j < n2 && haystack[i+j] == needle[j] ) j++;
if ( j == n2 ) ans = i;
}
return ans;
}
}
int main()
{
std::string haystack( "mississippi" );
std::string needle( "issip" );
std::cout << strStr( haystack, needle ) << '\n';
return 0;
}
Its output is
4

The problem is that you modify i in
if (haystack[i++] != needle[j]) {
Thus preventing a second potential match from being explored. Try
if (haystack[i + j] != needle[j]) {
and fix any knock-on issues. I expect it to work as-is, though.

Related

belonging of one string to another string

I have this function that checks if a string is a substring, is it possible for me to add a variable to it that will count to me how many times that subsequence appears in that sequence? or i need to create another function for that.
bool SearchString(string sir1, string sir2) {
if (sir2.size() > sir1.size())
return false;
for (int i = 0; i < sir1.size(); i++) {
int j = 0;
if (sir1[i] == sir2[j]) {
int k = i;
while (sir1[i] == sir2[j] && j < sir2.size()) {
j++;
i++;
}
if (j == sir2.size())
return true;
else
i = k;
}
}
return false;
}
As "500 - Internal Server Error" said, you simply need to increment a counter where you return. With a little bit of refactoring it would look like this:
unsigned SearchString(const string& haystack, const string& needle) {
if (needle.size() > haystack.size())
return 0;
unsigned count = 0;
for (int i = 0; i < haystack.size(); ++i) {
int j = 0;
if (haystack[i] == needle[j]) {
int k = i;
while (k < haystack.size() && j < needle.size() && haystack[k] == needle[j]) {
++j;
++k;
}
if (j == needle.size())
++count;
}
}
return count;
}
Note: it's important to check that you haven't reached the end of the haystack while searching for the needle. Consider haystack="ababa", needle="bac": trying to locate 'c' would read after the end of the haystack
It's also important to check reaching end before trying to dereference the next character:
while (sir1[i] == sir2[j] && j < sir2.size()) ...
would read sir2[j] before making sure j is not over the boundary.
For starters your function SearchString is too complicated and moreover can invoke undefined behavior in this loop
while (sir1[i] == sir2[j] && j < sir2.size()) {
j++;
i++;
}
if for example when the string s2 contains an imbedded zero character '\0'.
Also the function parameters should have constant referenced types.
The function can be written much simpler.
As for your question then it will be better to write a separate function to count occurrences of a sub-string.
That is there is no great sense to count all occurrences of a sub-string in a string if you need only to know whether the sub-string is present in the string.
Here is a demonstration program
#include <iostream>
#include <iomanip>
#include <string>
bool SearchString( const std::string &s1, const std::string &s2 )
{
return s1.find( s2 ) != std::string::npos;
}
size_t CountStringOccurrences( const std::string &s1, const std::string &s2 )
{
size_t n = 0;
for ( std::string::size_type pos = 0;
s1.find( s2, pos ) != std::string::npos;
pos += s2.size() )
{
++n;
}
return n;
}
int main()
{
std::string s1( "123123123" );
std::string s2( "123" );
std::cout << std::boolalpha << SearchString( s1, s2 ) << '\n';
std::cout << CountStringOccurrences( s1, s2 ) << '\n';
return 0;
}
The program output is
true
3
This sounds like std::search as in
bool SearchString(const std::string& s0, const std::string& s1) {
return std::search(s0.begin(), s0.end(), s1.begin(), s1.end()) != s0.end();
}
Counting would be a loop:
std::size_t count = 0;
for (auto it = std::search(s0.begin(), s0.end(), s1.begin(), s1.end());
it != s0.end();
it = std::search(it, s0.end(), s1.begin(), s1.end())) {
++count;
}
or perhaps clearer: pull out the duplication:
std::size_t count = 0;
auto search = [&](auto start) { return std::search(start, s0.end(), s1.begin(), s1.end()); };
for (auto it = search(s0.begin()); it != s0.end();
it = search(it)) {
++count;
}
I bet there’s a way to do it with std::ranges::count_if but I don’t see it.

what is wrong in my logic reversing vowels in a string?

I tried solving a problem in leetcode
which asks the programmer to reverse the vowels in the given string.
When I wrote my code in C, it ran fine and passed all the test cases.
I tried writing the same code in C++ but for a particular test case, it failed.
bool isVowel(char a)
{
if(a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return true;
if(a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U')
return true;
return false;
}
class Solution {
public:
string reverseVowels(string s) {
int i, j, k;
int len = s.length();
j = s.length() - 1;
i = 0;
k = 0;
string result;
//char result[len];
if (j < 0)
return s;
while(j >= 0) {
if (isVowel(s[j])) {
result[k] = s[j];
k++;
}
j--;
}
k = 0;
j = s.length() - 1;
while (i <= j) {
if(isVowel(s[i])) {
s[i] = result[k];
k++;
}
i++;
}
return s;
}
};
For some reason, when the input is "A new order began, a more Roman age bred Rowena." there is an error message AddressSanitizer: stack-buffer-overflow on address 0x7ffd4a543ab0 at pc 0x000000405efb.
When I tried to debug, I found that, the first while loop gets infinite. But when I replace the string result to char result[len], my code is working fine.
What is wrong in my approach?
Thanks
hago
You may not use the subscript operator for an empty string to change its value.
So your program has undefined behavior.
Pay attention to that in any case you are not reversing vowels in a string. You are trying to create a new string with reversed vowels from a given string. But this is not the same thing.
I can suggest the following Solution.:)
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
class Solution final
{
private:
static bool isVowel( char c )
{
const char *vowels = "AEIOU";
return std::strchr( vowels, std::toupper( static_cast<unsigned char>( c ) ) );
}
public:
static std::string & reverseVowels( std::string &s )
{
auto first = std::begin( s ), last = std::end( s );
do
{
while ( first != last && !isVowel( *first ) ) ++first;
if ( first != last )
{
while ( --last != first && !isVowel( *last ) );
}
if ( first != last ) std::iter_swap( first++, last );
} while ( first != last );
return s;
}
};
int main()
{
std::string s( "I am trying to write a program in C++" );
std::cout << s << '\n';
std::cout << Solution::reverseVowels( s ) << '\n';
return 0;
}
The program output is
I am trying to write a program in C++
i am tryong ta wreti o prigram In C++
Pay into account that the letter 'y' is not included in the set of vowels.
Your solution is correct but with a simple mistake.
When you declare string result; then this variable is declared with 0 size. So whenever you try to place character at some position (i.e result[0], result[1], ...) it finds that there is no allocated memory for this variable. So it throws error.
In stead of placing character to the result, you can add the character to this string.
So you can write result = result + s[j];
Code snap should be like this -
string result = "";
//char result[len];
if (j < 0)
return s;
while(j >= 0) {
if (isVowel(s[j])) {
result = result + s[j];
}
j--;
}
But adding character to a string takes more run-time.
Besides this, you can also use string.push_back() to add a single character to a string. It's complexity is overall O(n), n = length of the final string.
string result = "";
//char result[len];
if (j < 0)
return s;
while(j >= 0) {
if (isVowel(s[j])) {
result.push_back(s[j]);
}
j--;
}

Trouble with finding a c-string substring in C++

long time lurker, first time poster. I have been working on this problem for the last six hours hours.
Problem:
Implement the following functions. Each function deals with null terminated C-Style strings. You can assume that any char array passed into the functions will contain null terminated data. Place all of the functions in a single file and then create a main() function that tests the functions thoroughly.
Note: You may not use any c-string functions other than strlen().
I am having trouble with the fourth function.
The desired behavior is: This function returns the index in string s where the substring can first be found. For example if s is "Skyscraper" and substring is "ysc" the function would return 2. It should return -1 if the substring does not appear in the string.
prototype:
int findSubstring(char *str, char substring[])
Here's my two starts for function definitions, I'm not really sure if either is going in the right direction, I'm having a lot of trouble keeping the loop iterations in my head, any help would be TREMENDOUSLY appreciated.
int findSubstring(char *str, char substring[]){
int subS = -1, index1 = 0, index2 = 0;
int length1 = (strlen(str) - 1);
int length2 = (strlen(substring) - 1);
if(length1 > length2){
for(int i = 0; i <= length2; i++){
for(int j = 0; j <= length1; j++){
if(*(substring + i) == *(str + j) && *(substring +i) != '\0' ){
i++;
if(index1 == 0){
index1 = i;
}
}
if( *(substring + i) == '\0'){
subS = i + 2;
}
}
}
}
if (length1 < length2){
cout << "Invalid, substring exceeds size of string!" << endl;
}
return subS;
}
int findSubstring(char *str, char substring[]){
int index = -1;
int lengthStr = (strlen(str) - 1);
int lengthSub = (strlen(substring) - 1);
if (lengthStr < lengthSub){
cout << "Invalid input, substring exceeds size of string!" << endl;
}
if( lengthSub == 0){
cout << "";
}
if (lengthStr > lengthSub){
for(int i = 0; i <= lengthSub; i++){
for(int j = 0; j <= lengthStr; j++){
}
return index;
}
//You can replace my str.size() and subString.size() by the size of each c-string.
int stringPointerOperation( string str, string subString )
{
int pos=0;
bool notFound;
for(int i = 0; i < str.size() ; i++)
{
notFound= false;
if(str[i] == subString[0])
{
pos=i;
for(int k = 0 ; k < subString.size() && k < str.size() ; k++,i++)
{
if(subString[k] != str[i] )
{
notFound=true;
break;
}
}
}
}
if(notFound)
return -1;
else
return pos;
}
You are using the wrong strategy for finding a sub-string in a string. The outer for loop needs to iterate over the main string and the inner for loop needs to iterate over the sub-string.
Say you are looking for "de" in "abcdef". The strategy that I find easier to understand and implement is:
Can I find "de" starting from 0 of "abcdef". No, I can't.
Can I find "de" starting from 1 of "abcdef". No, I can't.
Can I find "de" starting from 2 of "abcdef". No, I can't.
Can I find "de" starting from 3 of "abcdef". Yes, I can. Return 3.
Here's a version that works for me.
int findSubstring(char *str, char substring[]){
int i;
int j;
int length1 = strlen(str);
int length2 = strlen(substring);
if(length1 < length2){
std::cout << "Invalid, substring exceeds size of string!" << std::endl;
return -1;
}
for(i = 0; i < length1; i++){
for(j = 0; j < length2; j++){
// The index to use access the element of str
// needs to be offset by i.
if( str[i+j] != substring[j] )
{
break;
}
}
if ( j == length2 )
{
return i;
}
}
return -1;
}

count the ocurrences of substrings

I've a task to count the occurrences of sub string in a char String. I write this code but on certain inputs output is wrong. like string is "hassana" and sub is "as" then it outputs 2 ...some one plz help me
int CharString :: countOccurenceOf(const char* substr)
{
int count = 0;
bool find = false;
for(int i = 0; i < size1; i++)
{
if(cstr[i] == substr[0])
{
int x = i;
int c = 1;
find = true;
while ( substr[c] != '\0' && find == true && (x+1) < size1)
{
if(cstr [x+1] != substr[c])
{
find = false;
}
c++;
x++;
}
if (find == true)
{
count++;
i = i + c-1;
}
}
}
return count;
}
Got some Solution.....is that okay?
int CharString :: countOccurenceOf(const char* substr)
{
int len = 0;
if ( substr != '\0')
{
while( substr[len] != '\0')
len++;
}
int count = 0;
bool find = false;
for(int i = 0; i < size1; i++)
{
if(cstr[i] == substr[0])
{
int x = i;
int c = 1;
find = true;
while ( substr[c] != '\0' && find == true && (x+1) < size1)
{
if(cstr [x+1] != substr[c])
{
find = false;
}
c++;
x++;
}
if (find == true && c == len)
{
count++;
i = i + c-1;
}
}
}
return count;
}
The problem is that you're breaking automatically if x+1 < size1. If the first character of the substring matches the last character of the main string, then this will automatically break and "find" will still be set to true so you'll increment matches by 1. There are numerous ways to change your code to fix this problem; hopefully you can find one now that you know what the problem is.
Assuming cstr is your class internal string:
int CharString :: countOccurenceOf(const char* substr)
{
int occurrencies = 0;
unsigned char* s = cstr;
while (s) {
if (strstr(s,substr)) { occurrencies++; s+= strlen(substr); }
else s++;
}
return occurrencies;
}

how to check whether 2 strings are rotations to each other ?

Given 2 strings, design a function that can check whether they are rotations to each other without making any changes on them ? The return value is boolean.
e.g ABCD, ABDC, they are not rotations. return false
ABCD, CDAB or DABC are rotations. return true.
My solution:
shift one of them to right or left one position and then compare them at each iteration.
If they are not equal at all iterations, return false. Otherwise, return true.
It is O(n). Are there other more efficient solutions ?
What if the contents of them cannot be changed ?
thanks
Concatenate the given string with the given string.
Search for the target string in the concatenated string.
Example:
Given = CDAB
After step 1, Concatenated = CDABCDAB
After step 2, Success CDABCDAB
^^^^
Rather than shifting one of them, it might be more efficient to use two index variables. Start one at 0 each time and the other at each of the possible positions (0 to N-1) and increment it mod N.
If you can't modify the strings, just take the first character of string1 and compare it to each character of string2. When you get a match, compare the second char of string1 to the next char of string2, and so on.
Pseudocode:
len = strlen(string1);
len2 = strlen(string2);
if( len != len2 )
printf("Nope.");
for( int i2=0; i2 < len; i2++ ) {
for( int i1=0; i1<len; i1++ ) {
if( string1[i1] != string2[(i2+i1)%len] )
break;
}
if( i1 == len ) {
print("Yup.");
break;
}
}
A simple one would be:
(s1+s1).find(s2) != string::npos && s1.size() == s2.size();
#include <iostream>
#include <cstring>
#include<string>
using namespace std;
void CompareString(string, string, int);
int ComputeStringLength(string str);
int main()
{
string str = ""; string str1 = ""; int len = 0, len1 = 0;
cout << "\nenter string ";
cin >> str;
cout << "\nenter string 2 to compare:- ";
cin >> str1;
len = ComputeStringLength(str);
len1 = ComputeStringLength(str1);
if (len == len1)
CompareString(str, str1, len);
else
cout << "rotation not possible";
getchar();
return 0;
}
int ComputeStringLength(string str)
{
int len = 0;
for (int i = 0; str[i] != '\0'; i++)
{
len++;
}
return len;
}
void CompareString(string str, string str1, int n)
{
int index = 0, flag = 0, curr_index = 0, count1 = 0, flagj = 0;
for (int i = 0; i<n; i++)
{
for (int j = flagj; j<n; j++)
{
if (str[i] == str1[j])
{
index = j;
flagj =j;
count1++;
flag++;
if (flag == 1)
{
curr_index = index;
}
break;
}
}
}
int temp = count1;
if (count1 != n)
{
if (curr_index>=0)
{
int k = 0;
for (int i = n - 1; i>n - curr_index - 1; i--)
{
if (str[i] == str1[k])
{
temp++;
k++;
}
}
}
if (temp == n)
{
cout << "\n\nstring is same after rotation";
}
else
{
cout << "\n\nstring is not same after rotation";
}
}
else
{
cout << "\n\nstring is same after rotation";
}
}
https://dsconceptuals.blogspot.in/2016/10/a-program-to-check-if-strings-are.html