Horje
find the smallest window in a string containing all characters of another string Code Example
find the smallest window in a string containing all characters of another string
#include <bits/stdc++.h>
using namespace std;
 
// Function
string Minimum_Window(string s, string t)
{
 
    int m[256] = { 0 };
 
    // Answer
    int ans = INT_MAX; // length of ans
    int start = 0; // starting index of ans
    int count = 0;
   
    // creating map
    for (int i = 0; i < t.length(); i++) {
        if (m[t[i]] == 0)
            count++;
        m[t[i]]++;
    }
 
    // References of Window
    int i = 0;
    int j = 0;
 
    // Traversing the window
    while (j < s.length()) {
        // Calculations
        m[s[j]]--;
        if (m[s[j]] == 0)
            count--;
 
        // Condition matching
        if (count == 0) {
            while (count == 0) {
                // Sorting ans
                if (ans > j - i + 1) {
                    ans = min(ans, j - i + 1);
                    start = i;
                }
                // Sliding I
                // Calculation for removing I
 
                m[s[i]]++;
                if (m[s[i]] > 0)
                    count++;
 
                i++;
            }
        }
        j++;
    }
 
    if (ans != INT_MAX)
        return s.substr(start, ans);
    else
        return "-1";
}
 
main()
{
    string s = "ADOBECODEBANC";
    string t = "ABC";
     
      cout<<"-->Smallest window that contain all character : "<<endl;
    cout << Minimum_Window(s, t);
}
find the smallest window in a string containing all characters of another string
#include <bits/stdc++.h>
using namespace std;
 
// Function
string Minimum_Window(string s, string t)
{
 
    int m[256] = { 0 };
 
    // Answer
    int ans = INT_MAX; // length of ans
    int start = 0; // starting index of ans
    int count = 0;
   
    // creating map
    for (int i = 0; i < t.length(); i++) {
        if (m[t[i]] == 0)
            count++;
        m[t[i]]++;
    }
 
    // References of Window
    int i = 0;
    int j = 0;
 
    // Traversing the window
    while (j < s.length()) {
        // Calculations
        m[s[j]]--;
        if (m[s[j]] == 0)
            count--;
 
        // Condition matching
        if (count == 0) {
            while (count == 0) {
                // Sorting ans
                if (ans > j - i + 1) {
                    ans = min(ans, j - i + 1);
                    start = i;
                }
                // Sliding I
                // Calculation for removing I
 
                m[s[i]]++;
                if (m[s[i]] > 0)
                    count++;
 
                i++;
            }
        }
        j++;
    }
 
    if (ans != INT_MAX)
        return s.substr(start, ans);
    else
        return "-1";
}
 
main()
{
    string s = "ADOBECODEBANC";
    string t = "ABC";
     
      cout<<"-->Smallest window that contain all character : "<<endl;
    cout << Minimum_Window(s, t);
}
find the smallest window in a string containing all characters of another string
#include <bits/stdc++.h>
using namespace std;
 
// Function
string Minimum_Window(string s, string t)
{
 
    int m[256] = { 0 };
 
    // Answer
    int ans = INT_MAX; // length of ans
    int start = 0; // starting index of ans
    int count = 0;
   
    // creating map
    for (int i = 0; i < t.length(); i++) {
        if (m[t[i]] == 0)
            count++;
        m[t[i]]++;
    }
 
    // References of Window
    int i = 0;
    int j = 0;
 
    // Traversing the window
    while (j < s.length()) {
        // Calculations
        m[s[j]]--;
        if (m[s[j]] == 0)
            count--;
 
        // Condition matching
        if (count == 0) {
            while (count == 0) {
                // Sorting ans
                if (ans > j - i + 1) {
                    ans = min(ans, j - i + 1);
                    start = i;
                }
                // Sliding I
                // Calculation for removing I
 
                m[s[i]]++;
                if (m[s[i]] > 0)
                    count++;
 
                i++;
            }
        }
        j++;
    }
 
    if (ans != INT_MAX)
        return s.substr(start, ans);
    else
        return "-1";
}
 
main()
{
    string s = "ADOBECODEBANC";
    string t = "ABC";
     
      cout<<"-->Smallest window that contain all character : "<<endl;
    cout << Minimum_Window(s, t);
}




Cpp

Related
ue4 c++ add tag Code Example ue4 c++ add tag Code Example
c++ program for inflation rate of two numbers Code Example c++ program for inflation rate of two numbers Code Example
Fibonacci in c++ Code Example Fibonacci in c++ Code Example
Define and show the implementation of the functions of an arrayList. Code Example Define and show the implementation of the functions of an arrayList. Code Example
how to initialize a vector of pairs in c++ Code Example how to initialize a vector of pairs in c++ Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
10