Medium csharp

Longest substring without repeat #95

Problem

Length of longest substring without repeating chars in "toolliyo".

Hints
  • Move start when duplicate found

Your solution

TestStatusDetails
Ready — edit the code above and click Run or Submit.

Solution

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        string s = "toolliyo";
        var seen = new Dictionary<char, int>();
        int best = 0, start = 0;
        for (int j = 0; j < s.Length; j++) {
            if (seen.ContainsKey(s[j]) && seen[s[j]] >= start) start = seen[s[j]] + 1;
            seen[s[j]] = j;
            best = Math.Max(best, j - start + 1);
        }
        Console.WriteLine(best);
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

Sliding window with hash map.

Discussion

0

Sign in to join the discussion.

No discussions yet — ask the first question!

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details