c# solution using queues


  • 0
    M
    public class Solution {
    public int CharacterReplacement(string s, int k) {
        var qs = new Queue<int>[26];
        int max = 0;
        for(int i = 0; i < 26; i++) {
            qs[i] = new Queue<int>();
            qs[i].Enqueue(-1);
        }
        for(int i = 0 ; i < s.Length; i++) {
            for(char c = 'A'; c <= 'Z'; c++) {
                var q = qs[c - 'A'];
                if(c != s[i]) {
                    if(q.Count > k)
                        q.Dequeue();
                    q.Enqueue(i);
                }
                max = Math.Max(max, i - q.Peek());
            }
        }
        return max;
    }
    

    }


Log in to reply
 

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