“Substring terpanjang tanpa karakter berulang” Kode Jawaban

Substring terpanjang tanpa karakter berulang

public class Solution 
{
    public int LengthOfLongestSubstring(string str) 
    {
        var dict = new Dictionary<char, int>();
        var max = 0;
        int start = 0;
        for (int i = 0; i < str.Length; i++)
        {
            var x = str[i];
            if (!dict.ContainsKey(x))
            {
                dict.Add(x, 1);
            }
            else
            {
                dict[x] += 1;
                while (start <= i && dict.ContainsKey(str[start]) && dict[x] > 1)
                {
                    dict[str[start]]--;
                    if (dict[str[start]] == 0)
                        dict.Remove(str[start]);
                    start++;
                }
            }
            max = Math.Max(max, i - start + 1);
        }
        return max;
    }
}
PrashantUnity

3. Substring terpanjang tanpa karakter berulang

import java.io.*;
 
class GFG {
    public static int longestUniqueSubsttr(String str)
    {
        String test = "";
 
        // Result
        int maxLength = -1;
 
        // Return zero if string is empty
        if (str.isEmpty()) {
            return 0;
        }
        // Return one if string length is one
        else if (str.length() == 1) {
            return 1;
        }
        for (char c : str.toCharArray()) {
            String current = String.valueOf(c);
 
            // If string already contains the character
            // Then substring after repeating character
            if (test.contains(current)) {
                test = test.substring(test.indexOf(current)
                                      + 1);
            }
            test = test + String.valueOf(c);
            maxLength = Math.max(test.length(), maxLength);
        }
 
        return maxLength;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        System.out.println("The input string is " + str);
 
        int len = longestUniqueSubsttr(str);
        System.out.println("The length of the longest "
                           + "non-repeating character "
                           + "substring is " + len);
    }
}
 
// This code is contributed by Alex Bennet
Manav Sarkar

Jawaban yang mirip dengan “Substring terpanjang tanpa karakter berulang”

Pertanyaan yang mirip dengan “Substring terpanjang tanpa karakter berulang”

Lebih banyak jawaban terkait untuk “Substring terpanjang tanpa karakter berulang” di C#

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya