Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions string_manipulation/longest.substring.no.repeat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Longest Substring Without Repeating Characters in R
# Author: sgindeed
# Description: Finds the length of the longest substring without repeating characters

# Ask for user input
input.string <- readline(prompt = "Enter a string: ")

# Convert the string to lowercase for case-insensitive processing (optional)
clean.string <- tolower(input.string)

# Split string into characters
chars <- strsplit(clean.string, "")[[1]]

# Initialize variables
hash.table <- list() # stores last index of characters
max.length <- 0
start <- 1 # start of current window

# Iterate over characters
for (i in seq_along(chars)) {
char <- chars[i]

# If character was seen before and is inside current window
if (!is.null(hash.table[[char]]) && hash.table[[char]] >= start) {
start <- hash.table[[char]] + 1 # move start to one after previous occurrence
}

# Update last seen index of the character
hash.table[[char]] <- i

# Update max length
current.length <- i - start + 1
if (current.length > max.length) {
max.length <- current.length
}
}

# Display the result
cat("Length of the longest substring without repeating characters:", max.length, "\n")