Longest K Distinct Substring

Sliding Window String Algorithm

Problem Description

Given a string s and an integer k, find the length of the longest substring that contains at most k distinct characters. If there is no valid substring, return 0.

Example 1:

Input: s = "eceba", k = 2
Output: 3
Explanation: The substring is "ece" with 2 distinct characters.

Example 2:

Input: s = "aa", k = 1
Output: 2
Explanation: The substring is "aa" with 1 distinct character.

Notes

  • Try to solve the problem using the sliding window technique to achieve an optimal solution.
  • This challenge ties into the idea of finding balance, a concept often celebrated in events like February's community and cultural holidays.

Starter Code

Below is some starter code for various programming languages. The function signature is provided along with comments explaining the input and expected output.

// JavaScript
// Function: longestKDistinct(s: string, k: number) -> number
function longestKDistinct(s, k) {
    // Write your code here
}

// Example usage:
// console.log(longestKDistinct('eceba', 2)); // Expected output: 3
// TypeScript
// Function: longestKDistinct(s: string, k: number) => number
function longestKDistinct(s: string, k: number): number {
    // Write your code here
    return 0;
}

// Example usage:
// console.log(longestKDistinct('eceba', 2)); // Expected output: 3
<?php
// PHP
// Function: longestKDistinct(string $s, int $k): int
function longestKDistinct(string $s, int $k): int {
    // Write your code here
    return 0;
}

// Example usage:
// echo longestKDistinct('eceba', 2); // Expected output: 3
?>
// C#
// Function: int LongestKDistinct(string s, int k)
public int LongestKDistinct(string s, int k) {
    // Write your code here
    return 0;
}

// Example usage:
// Console.WriteLine(LongestKDistinct("eceba", 2)); // Expected output: 3
// C++
// Function: int longestKDistinct(string s, int k)
#include <string>
using namespace std;

int longestKDistinct(string s, int k) {
    // Write your code here
    return 0;
}

// Example usage:
// cout << longestKDistinct("eceba", 2) << endl; // Expected output: 3
// Java
// Function: public int longestKDistinct(String s, int k)
public int longestKDistinct(String s, int k) {
    // Write your code here
    return 0;
}

// Example usage:
// System.out.println(longestKDistinct("eceba", 2)); // Expected output: 3
-- Lua
-- Function: longestKDistinct(s: string, k: number) -> number
local function longestKDistinct(s, k)
    -- Write your code here
    return 0
end

-- Example usage:
-- print(longestKDistinct('eceba', 2)) -- Expected output: 3
# Python
# Function: longestKDistinct(s: str, k: int) -> int
def longestKDistinct(s: str, k: int) -> int:
    # Write your code here
    return 0

# Example usage:
# print(longestKDistinct('eceba', 2))  # Expected output: 3
# Ruby
# Function: longest_k_distinct(s, k) -> integer
def longest_k_distinct(s, k)
  # Write your code here
  0
end

# Example usage:
# puts longest_k_distinct('eceba', 2)  # Expected output: 3