0

What is the length of the longest substring containing only 1 after setting at most one char to 1?
For example, given s="1101100111", the answer is 5

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int Largeststring(string nums) {
    int left = 0, curr = 0, ans = 0;
    
    for (int right = 0; right < nums.size(); right++) {
        if (nums[right] == "0") {
            curr++;
        }

        while (curr > 0) {
            if (nums[left] == "0") {
                curr--;
            }
        }
        
        ans = max(ans, right - left + 1);
    }
}

int main()
{
    string s = "1101100111";
    cout << Largeststring(s);
}

In this code it gives the compile time error

"error: ISO C++ forbids comparison between pointer and integer [-fpermissive]" for the lines "nums[right] == "0" and the similar line for nums left.

Firstly, in this case I don't understand what the pointer is and there is no integer as well, I thought it was because I was calling by reference it was giving this error, but I switched it to call by value, but the error seems to persist

So initially I tried using a char vector, I thought the error was that because characters are initialised individually to the char vector, but when I switched to string the same problem seems to exist.
Kindly assist me in this problem.

greybeard
  • 2,249
  • 8
  • 30
  • 66
  • What is "one operation?" You haven't stated the problem clearly, so it's not at all clear what you're trying to achieve or what might be wrong with the code you've written to achieve it. – Jim Mischel May 26 '23 at 17:09

2 Answers2

1

My C / C++ days were quit some years ago, but let's have a look at the problematic line:

nums[left] == "0"

nums[left] is a char (so a kind of integer)
"0" is a string literal (therefore a kind of pointer to chars)

So == tries to compare the pointer "0" with the integer nums[left]. That's what the error message wants to tell you.

The solution should be:

nums[left] == '0'

'0' is a character so now you compare two characters (so a kind of integers).

greybeard
  • 2,249
  • 8
  • 30
  • 66
MrSmith42
  • 9,961
  • 6
  • 38
  • 49
0

You have several problems with your code

  1. Syntactic: nums[i] is a char, not string so it should be nums[i] == '0'
  2. Syntactic: you should return function's result: return ans;
  3. Algorithmic: your code fails on 101

Let's scan the string while trying to change each 0 into 1 and then return maximum result:

int Largeststring(string nums){
  int result = 0;   /* longest required substring so far */
  int current = 0;  /* current all ones substring */ 
  int startAt = -1; /* where current starts (last zero) */
        
  for (int i = 0; i < nums.size(); ++i) {
    if (nums[i] == '1') /* note that i-th item is a char, not (sub-)string */
      current += 1; /* one more 1 */
    else {          
      current = i - startAt; /* restart from the earlier 1 */
      startAt = i;
    }
            
    result = max(current, result);
  }
        
  return result; /* do not forget to return the answer */
}

Please, fiddle yourself

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215