• + 0 comments

    I managed to optimize the space using 1D array.

    #include <iostream>
    #include <cstring> 
    using namespace std;
    
    bool mem[1001]; 
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        int t; 
        cin >> t; 
    
        string a, b; 
        while(t--) {
            cin >> a >> b; 
            memset(mem, 0, b.size() + 1); 
            mem[0] = true; 
    
            for(int i = 0; i < a.size(); ++i) {
                char curr = toupper(a[i]); 
                bool is_upper = isupper(a[i]); 
    
                for(int j = b.size(); j > -1; --j) {
                    if(mem[j]) {
                        if(j < b.size() && curr == b[j]) mem[j + 1] = true; 
                        if(is_upper) mem[j] = false; 
                    }
                }
            }
    
            cout << (mem[b.size()]? "YES" : "NO") << '\n'; 
        }
    
        return 0; 
    }