Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Longest Substring Without Repeating Characters
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { String str = "abcxylmzabc"; Console.WriteLine("The input string is " + str); int len = longestUniqueSubsttr(str); Console.Write("The length of the longest non repeating character is " + len); } static int NO_OF_CHARS = 256; public static int longestUniqueSubsttr(String str) { int n = str.Length; // length of current substring int cur_len = 1; // result int max_len = 1; // previous index int prev_index; int i; int[] visited = new int[NO_OF_CHARS]; /* Initialize the visited array as -1, -1 is used to indicate that character has not been visited yet. */ for (i = 0; i < NO_OF_CHARS; i++) { visited[i] = -1; } /* Mark first character as visited by storing the index of first character in visited array. */ visited[str[0]] = 0; /* Start from the second character. First character is already processed (cur_len and max_len are initialized as 1, and visited[str[0]] is set */ for (i = 1; i < n; i++) { prev_index = visited[str[i]]; /* If the current character is not present in the already processed substring or it is not part of the current NRCS, then do cur_len++ */ Console.WriteLine(" i- cur_len > prev_index : {5} - {0} > {1} ---- [{2}] = {3} ------ prev_index = {4} ", cur_len, prev_index, i, str[i], prev_index, i); if (prev_index == -1 || i - cur_len > prev_index) cur_len++; /* If the current character is present in currently considered NRCS, then update NRCS to start from the next character of the previous instance. */ else { /* Also, when we are changing the NRCS, we should also check whether the length of the previous NRCS was greater than max_len or not.*/ if (cur_len > max_len) { max_len = cur_len; } cur_len = i - prev_index; } // update the index of current character visited[str[i]] = i; } // Compare the length of last NRCS with max_len and update max_len if needed if (cur_len > max_len) max_len = cur_len; return max_len; } } }
run
|
edit
|
history
|
help
0
hello...
String combinations
csharp enum
https://www.usgatmtu.mtu.edu/profile/watch-the-bad-guys-online-streaming/profile
C#: Strings / Char Test
Typeing
Assignment
new
prime numbers 1-100
linear search