0

I have provided the entire code for context:

#include <bits/stdc++.h> 
using namespace std; 

const int MAX_CHARS = 256; 

// Function to find smallest window containing 
// all distinct characters 
string findSubString(string str) 
{ 
    int n = str.length(); 

    // Count all distinct characters. 
    int dist_count = 0; 
    bool visited[MAX_CHARS] = { false }; 
    for (int i = 0; i < n; i++) { 
        if (visited[str[i]] == false) { 
            visited[str[i]] = true; 
            dist_count++; 
        } 
    } 


    int start = 0, start_index = -1, min_len = INT_MAX; 

    int count = 0; 
    int curr_count[MAX_CHARS] = { 0 }; 
    for (int j = 0; j < n; j++) { 
        // Count occurrence of characters of string 
        curr_count[str[j]]++; 

        // If any distinct character matched, 
        // then increment count 
        if (curr_count[str[j]] == 1) 
            count++; 

        // if all the characters are matched 
        if (count == dist_count) { 

            // the section where i got it wrong
            while (curr_count[str[start]] > 1) {      
                if (curr_count[str[start]] > 1) 
                    curr_count[str[start]]--; 
                start++;
            } 
          // end section where i got it wrong

            // Update window size 
            int len_window = j - start + 1; 
            if (min_len > len_window) { 
                min_len = len_window; 
                start_index = start; 
            } 
        } 
    } 

    // Return substring starting from start_index 
    // and length min_len 
    return str.substr(start_index, min_len); 
} 

// Driver code 
int main() 
{ 
    string str = "aabcbcdbca"; 
    cout << "Smallest window containing all distinct"
            " characters is: "
        << findSubString(str); 
    return 0; 
} 

please correct me if i'm wrong. The while loop already checks for the condition where count(current_char)>1. If that's the case what is the need to check for IF?

        //code snippet 1:
        while (curr_count[str[start]] > 1) { 
                        if (curr_count[str[start]] > 1) 
                            curr_count[str[start]]--; 
                        start++; 
                    } 


        // code snippet 2:
        while (curr_count[str[start]] > 1) { 

                        curr_count[str[start]]--; 
                        start++; 
                    }

input: aabcbcdbca
output(considering snippet 1) : 4
output(considering snippet 2) : 6 // the start pointer wasn't incrementing

Jarod42
  • 203,559
  • 14
  • 181
  • 302
otaku2310
  • 31
  • 5
  • 1
    The 2 snippets should be identical, they give me the same results. Are you sure that's the only difference. Is the `start++;` within some `if` braces? – cigien May 10 '20 at 17:32
  • Got same results as expected [here](https://coliru.stacked-crooked.com/a/455e21ab704f39ea). – Jarod42 May 10 '20 at 17:37
  • 3
    **Recommended reading:** [Why should I not #include ?](https://stackoverflow.com/q/31816095/560648) – Asteroids With Wings May 10 '20 at 17:38
  • 1
    There may be a tiny error in your code that is being hidden by that fact that you're using **all** STL headers (`#include `) and `using namespace std;` (two seriously dodgy practices). Try only using those you need: `#include #include using std::string; using std::cout;` – Adrian Mole May 10 '20 at 17:40
  • Also, how are your outputs `4` and `6`? Your code outputs a `string` result. – Adrian Mole May 10 '20 at 17:47
  • 1
    Why do so many people lately ask questions where they use '#include `? Why do you use that include? It is not a C++ standard include file. It might exist on your platform but not on other platforms. – Werner Henze May 10 '20 at 18:01
  • @WernerHenze: I would say convenience/laziness, and as frequently (VLA, using namespace, globals, pass by copy, ...), that path is rarely the right way. – Jarod42 May 10 '20 at 18:20
  • @Jarod42 But where do you learn to include ? I have never read about this file before, saw it only here in questions lately. There must be a source where people learn it. – Werner Henze May 10 '20 at 18:23
  • @cigen oh. yes im sure when i tried it on g4g ide, it produced different results. thank u for checking! – otaku2310 May 11 '20 at 11:34
  • @Jarod42 oh thanku! would look into it – otaku2310 May 11 '20 at 11:35
  • @Asteroids With Wings thanku! would refer it n not use that header in the future – otaku2310 May 11 '20 at 11:36
  • @Adrian Mole oh ok. would use only the required headers from now on. thanku! actually was supposed to print just the minimum length, sorry. – otaku2310 May 11 '20 at 11:36
  • @Werner Henze oh i'm sorry didn't know that. would keep in mind. thankyou! – otaku2310 May 11 '20 at 11:39

0 Answers0