generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem50.cs
More file actions
55 lines (46 loc) · 1.66 KB
/
Problem50.cs
File metadata and controls
55 lines (46 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/longest-repeating-character-replacement/">Longest Repeating Character Replacement</see>.
/// </summary>
public static class Problem50
{
/// <summary>
/// You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character.
/// You can perform this operation at most k times.
/// Return the length of the longest substring containing the same letter you can get after performing the above operations.
/// Time complexity: O(n).
/// Space complexity: O(1).
/// </summary>
/// <param name="s">String to traverse.</param>
/// <param name="k">k.</param>
/// <returns>Length of the longest substring containing the same letter.</returns>
public static int CharacterReplacement(string s, int k)
{
var maxLength = 0;
if (string.IsNullOrEmpty(s))
{
return maxLength;
}
var dict = new Dictionary<char, int>(26);
for (int left = 0, right = 0, maxFrequency = 0; right < s.Length; right++)
{
var character = s[right];
if (dict.ContainsKey(character))
{
dict[character]++;
}
else
{
dict.Add(character, 0);
}
maxFrequency = Math.Max(maxFrequency, dict[character]);
while (right - left - maxFrequency > k)
{
dict[s[left]]--;
left++;
}
maxLength = Math.Max(maxLength, right - left);
}
return maxLength + 1;
}
}