Run-Length Encoding
Function Name: runLengthEncode
Description
Implement a function to perform run-length encoding on a string.
Run-length encoding is a basic form of data compression, where sequences of the same data value (in this case, characters) are stored as a single data value and a count.
This function should take a string as input and return the encoded string.
The output string should be formed by concatenating each unique character with the number of times it appears consecutively in the input string.
The function should be case-sensitive ('A' is considered different from 'a').
If a character occurs only once consecutively, it should be followed by the number '1'.
The function must handle empty strings gracefully by returning an empty string.
Requirements
- The function must take exactly one parameter: the string to be encoded.
- No external libraries or modules should be used; rely on standard language features to solve the challenge.
- The input string will contain only printable ASCII characters.
- The function should be optimized for readability and not for the ultimate performance.
- Consider edge cases, such as empty strings and strings with single-character repetitions.
Examples
Given the string 'aabcccccaaa', the function should return 'a2b1c5a3'.Given an empty string '', the function should return an empty string.Given the string 'abcd', the function should return 'a1b1c1d1'.
Links
Balanced Parentheses Checker
Wed Nov 06 2024
Write a function that takes a string containing only parentheses characters '(', ')', '{', '}', '[' and ']', and determines if the input string is valid.An input string is valid if:- Open brackets are closed by the same type of brackets.- Open brackets are closed in the correct order.No extra characters are allowed in the input string.
Binary Tree Right Side View
Fri Nov 08 2024
Write a function that takes the root node of a binary tree and returns the value of the nodes visible when the tree is viewed from the right side. This is done by returning the rightmost node at each level of the tree. If no rightmost node is present at any level (such as in a left-leaning tree), the function should return the last node in that level's traversal.