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

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

KQL Find First Non-Repeating Character

Kusto Query Language (KQL)

Goal -- WPM

Ready
Exercise Algorithm Area
1let findFirstNonRepeatingChar = (inputString: string) => {
2if (strlen(inputString) == 0) {
3return -1; // Handle empty string
4}
5
6let charCounts = datatable(char: char, count: int) [];
7let processedChars = '';
8
9// Count character frequencies
10for (i = 0; i < strlen(inputString); i++) {
11let currentChar = substring(inputString, i, 1);
12let currentCount = array_length(strcat(processedChars, currentChar)) - array_length(processedChars);
13if (currentCount > 0) {
14// Character already seen, increment count
15let existingRow = charCounts | where char == currentChar;
16if (cardinality(existingRow) > 0) {
17charCounts = charCounts | union (charCounts | where char == currentChar | extend count = count + 1);
18} else {
19// First occurrence, add to table
20charCounts = charCounts | union (datatable(char: char, count: int) [currentChar, 1]);
21}
22} else {
23// First occurrence, add to table
24charCounts = charCounts | union (datatable(char: char, count: int) [currentChar, 1]);
25}
26processedChars = strcat(processedChars, currentChar);
27}
28
29// Find the first character with a count of 1
30for (i = 0; i < strlen(inputString); i++) {
31let currentChar = substring(inputString, i, 1);
32let count = charCounts | where char == currentChar | project count;
33if (count == 1) {
34return i;
35}
36}
37
38return -1; // All characters repeat
39};
40
41// Example usage:
42// print findFirstNonRepeatingChar("leetcode");
43// print findFirstNonRepeatingChar("loveleetcode");
44// print findFirstNonRepeatingChar("aabb");
45// print findFirstNonRepeatingChar("");
Algorithm description viewbox

KQL Find First Non-Repeating Character

Algorithm description:

This KQL query defines a function to find the index of the first character that appears only once in a given string. It iterates through the string, counts the occurrences of each character using a temporary table, and then iterates again to find the first character with a count of one. This is useful for text processing tasks where identifying unique elements is important.

Algorithm explanation:

The `findFirstNonRepeatingChar` function first handles the edge case of an empty input string by returning -1. It then iterates through the string to build a frequency count of each character. A temporary table `charCounts` stores each character and its corresponding count. After counting, it iterates through the string a second time. For each character, it checks its count in `charCounts`. If the count is 1, the index of that character is returned. 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 table operations are generally efficient for small alphabets. The space complexity is O(K) where K is the number of unique characters in the string, for storing character counts.

Pseudocode:

FUNCTION findFirstNonRepeatingChar(inputString):
  IF inputString is empty THEN RETURN -1

  INITIALIZE empty charCounts table
  INITIALIZE empty processedChars string

  FOR i FROM 0 TO length(inputString) - 1:
    currentChar = character at index i in inputString
    IF currentChar is NOT in processedChars THEN
      Add currentChar with count 1 to charCounts
    ELSE
      Increment count for currentChar in charCounts
    END IF
    Append currentChar to processedChars
  END FOR

  FOR i FROM 0 TO length(inputString) - 1:
    currentChar = character at index i in inputString
    IF count of currentChar in charCounts is 1 THEN RETURN i
  END FOR

  RETURN -1