C# solution: simply compare with two charAndCount


  • 0
    B
    public class Solution 
    {
        public bool CheckInclusion(string s1, string s2) 
        {
            var s1CharAndCount = new int[26];
    
            for (int i = 0; i < s1.Length; i++)
            {
                var curChar = s1[i];
                s1CharAndCount[curChar - 'a']++;
            }
    
            for (int i = s1.Length - 1; i < s2.Length; i++)
            {
                var s2CharAndCount = new int[26];
                for (int j = i; j >= i - s1.Length + 1; j--)
                {
                    var curChar = s2[j];
                    s2CharAndCount[curChar - 'a']++;
                }
    
                var hasPermutation = true;
                for (int j = 0; j < s1CharAndCount.Length; j++)
                {
                    if (s1CharAndCount[j] != s2CharAndCount[j])
                    {
                        hasPermutation = false;
                        break;
                    }
                }
    
                if (hasPermutation) return true;
            }
    
            return false;
        }
    }
    

Log in to reply
 

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