Count Substrings That Can Be Rearranged to Contain a String I

problem

class Solution {
public long validSubstringCount(String word1, String word2) {
long count = 0;

int left = 0, right = 0;
int arr[] = new int[26];
for (int i = 0; i < word2.length(); i++) {


This content originally appeared on DEV Community and was authored by Prashant Mishra

problem



class Solution {
    public long validSubstringCount(String word1, String word2) {
        long count = 0;

        int left = 0, right = 0;
        int arr[] = new int[26];
        for (int i = 0; i < word2.length(); i++) {
            arr[word2.charAt(i) - 'a']++;
        }
        int b[] = new int[26];
        while (right < word1.length()) {
            b[word1.charAt(right) - 'a']++;
            while(satisfy(b, arr)) {
                count += word1.length() - right;
                b[word1.charAt(left) - 'a']--;
                left++;
            }

            right++;
        }
        return count;
    }

    public boolean satisfy(int b[], int w[]) {

        for (int i = 0; i < 26; i++) {
            if (w[i] > b[i])
                return false;
        }
        // System.out.println(sb.toString());
        return true;
    }
}



This content originally appeared on DEV Community and was authored by Prashant Mishra


Print Share Comment Cite Upload Translate Updates
APA

Prashant Mishra | Sciencx (2024-10-05T05:34:51+00:00) Count Substrings That Can Be Rearranged to Contain a String I. Retrieved from https://www.scien.cx/2024/10/05/count-substrings-that-can-be-rearranged-to-contain-a-string-i/

MLA
" » Count Substrings That Can Be Rearranged to Contain a String I." Prashant Mishra | Sciencx - Saturday October 5, 2024, https://www.scien.cx/2024/10/05/count-substrings-that-can-be-rearranged-to-contain-a-string-i/
HARVARD
Prashant Mishra | Sciencx Saturday October 5, 2024 » Count Substrings That Can Be Rearranged to Contain a String I., viewed ,<https://www.scien.cx/2024/10/05/count-substrings-that-can-be-rearranged-to-contain-a-string-i/>
VANCOUVER
Prashant Mishra | Sciencx - » Count Substrings That Can Be Rearranged to Contain a String I. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/05/count-substrings-that-can-be-rearranged-to-contain-a-string-i/
CHICAGO
" » Count Substrings That Can Be Rearranged to Contain a String I." Prashant Mishra | Sciencx - Accessed . https://www.scien.cx/2024/10/05/count-substrings-that-can-be-rearranged-to-contain-a-string-i/
IEEE
" » Count Substrings That Can Be Rearranged to Contain a String I." Prashant Mishra | Sciencx [Online]. Available: https://www.scien.cx/2024/10/05/count-substrings-that-can-be-rearranged-to-contain-a-string-i/. [Accessed: ]
rf:citation
» Count Substrings That Can Be Rearranged to Contain a String I | Prashant Mishra | Sciencx | https://www.scien.cx/2024/10/05/count-substrings-that-can-be-rearranged-to-contain-a-string-i/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.