Java, Beats 98.84%


  • 0
    K

    class Solution {

    public boolean checkInclusion(String s1, String s2) {
        int[] table = new int[256];
        for (char ch: s1.toCharArray()) {
            table[ch]++;
        }
        
        int i = 0;
        int j = 0;
        int len = s2.length();
        int match = s1.length();
        
        while (j < len) {
            char ch = s2.charAt(j);
            
            if (table[ch] > 0) {
                table[ch]--;
                match--;
                
                if (match == 0) {
                    return true;
                }
            } else {
                char next = s2.charAt(i);
                while (next != ch) {
                    table[next]++;
                    match++;
                    next = s2.charAt(++i);
                }
                i++;
            }
            j++;
        }
        return false;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.