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
31 changes: 31 additions & 0 deletions string_manipulation/burrows.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Burrows-Wheeler Transform (BWT) in R
# Computes the Burrows-Wheeler Transform of a string
# Useful in compression and efficient substring searching

burrows_wheeler_transform <- function(s) {
s <- paste0(s, "$") # Append unique end-of-string character
n <- nchar(s)

# Generate all rotations of the string
rotations <- character(n)
for (i in 1:n) {
rotations[i] <- paste0(substr(s, i, n), substr(s, 1, i - 1))
}

# Sort the rotations lexicographically
rotations_sorted <- sort(rotations)

# Build BWT by taking the last character of each sorted rotation
bwt <- paste0(sapply(rotations_sorted, function(x) substr(x, n, n)), collapse = "")

return(list(original = s, rotations_sorted = rotations_sorted, bwt = bwt))
}

# Interactive input
s <- readline(prompt = "Enter a string: ")
result <- burrows_wheeler_transform(s)

cat("Original string with end marker: ", result$original, "\n")
cat("Sorted rotations:\n")
print(result$rotations_sorted)
cat("Burrows-Wheeler Transform: ", result$bwt, "\n")