ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

PL/SQL: Find First Non-Repeating Character in String

PL/SQL

Goal -- WPM

Ready
Exercise Algorithm Area
1CREATE OR REPLACE FUNCTION find_first_non_repeating_idx (
2p_input_string IN VARCHAR2
3) RETURN NUMBER
4IS
5v_char_counts DBMS_UTILITY.uncl_array;
6v_string_length NUMBER;
7v_char VARCHAR2(1);
8v_idx NUMBER;
9BEGIN
10-- Handle edge case: empty or null input string
11IF p_input_string IS NULL OR LENGTH(p_input_string) = 0 THEN
12RETURN -1; -- Indicate no non-repeating character found
13END IF;
14
15v_string_length := LENGTH(p_input_string);
16
17-- Initialize character counts using a helper function
18v_char_counts := initialize_char_counts(p_input_string);
19
20-- Iterate through the string to find the first non-repeating character
21FOR v_idx IN 1 .. v_string_length LOOP
22v_char := SUBSTR(p_input_string, v_idx, 1);
23
24-- Check if the count for this character is 1
25IF v_char_counts(v_char) = 1 THEN
26RETURN v_idx; -- Return the 1-based index
27END IF;
28END LOOP;
29
30-- If no non-repeating character is found after iterating through the string
31RETURN -1;
32END;
33
34-- Helper function to initialize and populate character counts
35CREATE OR REPLACE FUNCTION initialize_char_counts (
36p_input_string IN VARCHAR2
37) RETURN DBMS_UTILITY.uncl_array
38IS
39v_char_counts DBMS_UTILITY.uncl_array;
40v_string_length NUMBER;
41v_char VARCHAR2(1);
42v_idx NUMBER;
43BEGIN
44v_string_length := LENGTH(p_input_string);
45
46-- Populate the counts for each character in the string
47FOR v_idx IN 1 .. v_string_length LOOP
48v_char := SUBSTR(p_input_string, v_idx, 1);
49-- Increment count for the character
50v_char_counts(v_char) := NVL(v_char_counts(v_char), 0) + 1;
51END LOOP;
52
53RETURN v_char_counts;
54END;
Algorithm description viewbox

PL/SQL: Find First Non-Repeating Character in String

Algorithm description:

This PL/SQL function identifies the index of the first character in a string that appears only once. It's useful in text processing tasks, such as data validation or finding unique identifiers within log entries. For example, in a string like 'leetcode', 'l' is the first non-repeating character at index 1.

Algorithm explanation:

The function first handles edge cases by returning -1 for null or empty input strings. It then uses a helper function, `initialize_char_counts`, to create a frequency map (simulated using `DBMS_UTILITY.uncl_array`) of all characters in the input string. This map stores each unique character as a key and its occurrence count as the value. The main function then iterates through the input string again, character by character. For each character, it checks its count in the frequency map. If a character's count is exactly 1, its current index (1-based) is immediately returned, as this signifies the first non-repeating character. If the loop completes without finding such a character, it means all characters repeat, and -1 is returned. The time complexity is O(N) because we iterate through the string twice, and the space complexity is O(K), where K is the number of unique characters in the string (at most 256 for ASCII, or more for Unicode, but bounded).

Pseudocode:

FUNCTION find_first_non_repeating_idx(input_string):
  IF input_string is NULL or empty THEN
    RETURN -1
  END IF

  char_counts = initialize_char_counts(input_string)

  FOR each character at index i in input_string:
    IF char_counts[character] == 1 THEN
      RETURN i
    END IF
  END FOR

  RETURN -1

FUNCTION initialize_char_counts(input_string):
  counts = empty map
  FOR each character at index i in input_string:
    increment counts[character]
  END FOR
  RETURN counts