File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ # Longest Substring Without Repeating Characters in R
2+ # Author: sgindeed
3+ # Description: Finds the length of the longest substring without repeating characters
4+
5+ # Ask for user input
6+ input.string <- readline(prompt = " Enter a string: " )
7+
8+ # Convert the string to lowercase for case-insensitive processing (optional)
9+ clean.string <- tolower(input.string )
10+
11+ # Split string into characters
12+ chars <- strsplit(clean.string , " " )[[1 ]]
13+
14+ # Initialize variables
15+ hash.table <- list () # stores last index of characters
16+ max.length <- 0
17+ start <- 1 # start of current window
18+
19+ # Iterate over characters
20+ for (i in seq_along(chars )) {
21+ char <- chars [i ]
22+
23+ # If character was seen before and is inside current window
24+ if (! is.null(hash.table [[char ]]) && hash.table [[char ]] > = start ) {
25+ start <- hash.table [[char ]] + 1 # move start to one after previous occurrence
26+ }
27+
28+ # Update last seen index of the character
29+ hash.table [[char ]] <- i
30+
31+ # Update max length
32+ current.length <- i - start + 1
33+ if (current.length > max.length ) {
34+ max.length <- current.length
35+ }
36+ }
37+
38+ # Display the result
39+ cat(" Length of the longest substring without repeating characters:" , max.length , " \n " )
You can’t perform that action at this time.
0 commit comments