Find First Non-Repeating Character
CREATE OR REPLACE FUNCTION find_first_non_repeating_char(input_string TEXT) RETURNS TEXT AS $$ DECLARE char_counts INT[256]; -- Assuming ASCII characters i INT; current_char CHAR(1); result CHAR(1) := NULL; BEGIN -- Initialize character counts to zero FOR i IN 0..255 LOOP char_co...
This SQL function, `find_first_non_repeating_char`, identifies the first character in a given string that appears only once. It iterates through the string twice: first to count the occurrences of each character and then...
The function uses an array `char_counts` to store the frequency of each character, indexed by their ASCII values. The time complexity is O(N), where N is the length of the input string, because we iterate through the string twice. The space complexity is O(1) because the size of the `char_counts` array is fixed (256 for ASCII), regardless of the input string's length. The algorithm handles empty or NULL input strings by returning NULL. It correctly identifies the first non-repeating character by maintaining the order of appearance in the second pass.
FUNCTION find_first_non_repeating_char(input_string): IF input_string is NULL or empty THEN RETURN NULL END IF CREATE an array `char_counts` of size 256, initialized to 0. FOR each character `c` in input_string: INCREMEN...