First Unique Character in a String

🏠 ⬅️ ➡️

Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

Example 1:

Input: s = "leetcode" Output: 0

Example 2:

Input: s = "loveleetcode" Output: 2

Example 3:

Input: s = "aabb" Output: -1

Constraints:

  • 1 <= s.length <= 105
  • s consists of only lowercase English letters.

Note: This problem is from LeetCode.
Compiled
Executed
⚠️ Correct
program main
    implicit none

    integer :: i, j, k, n, m
    character(len=100) :: s
    logical :: found

    ! Example 1
    s = 'leetcode'
    print *, find_first_non_repeating_char(s)

    ! Example 2
    s = 'loveleetcode'
    print *, find_first_non_repeating_char(s)

    ! Example 3
    s = 'aabb'
    print *, find_first_non_repeating_char(s)

contains

    function find_first_non_repeating_char(str) result(index)
        implicit none
        character(len=*), intent(in) :: str
        integer :: index
        logical :: found

        index = -1
        do i = 1, len(str)
            found = .false.
            do j = 1, len(str)
                if (i /= j) then
                    if (str(i:i) == str(j:j)) then
                        found = .true.
                        exit
                    end if
                end if
            end do
            if (.not. found) then
                index = i
                exit
            end if
        end do
    end function find_first_non_repeating_char

end program main
Compiled
Executed
Correct
! Declare variables
integer :: i, j, len, first_index
character(len=105) :: s

! Read input
read(*,*) s

! Initialize variables
len = len(s)
first_index = -1

! Loop through the string
do i = 1, len
    ! Check if the current character is already in the string
    do j = 1, i - 1
        if (s(i:i) == s(j:j)) then
            cycle
        end if
    end do

    ! If the current character is not in the string, set the first index
    if (first_index == -1) then
        first_index = i
    end if
end do

! Output the result
write(*,*) first_index

end
🌐 Data from online sources
def firstUniqChar(s):
    char_count = {}
    for c in s:
        char_count[c] = char_count.get(c, 0) + 1
    for i in range(len(s)):
        if char_count[s[i]] == 1:
            return i
    return -1
The algorithm first creates a dictionary to store the count of each character in the input string 's'. Then, it iterates through the string, counting the occurrences of each character and updating the dictionary accordingly.

After that, the algorithm iterates through the input string 's' again, and it checks if the count of the current character in the dictionary is 1. If it is, the algorithm returns the index of the current character as the answer. If no such character is found, the algorithm returns -1 as the answer.

The time complexity of this algorithm is O(n), where n is the length of the input string 's'.

🌐 Data from online sources
int firstUniqChar(std::string s) {
    std::unordered_map<char, int> charCount;
    for (char c : s)
        charCount[c]++;
    for (int i = 0; i < s.length(); i++)
        if (charCount[s[i]] == 1)
            return i;
    return -1;
}
The algorithm first creates a dictionary to store the count of each character in the input string 's'. Then, it iterates through the string, counting the occurrences of each character and updating the dictionary accordingly.

After that, the algorithm iterates through the input string 's' again, and it checks if the count of the current character in the dictionary is 1. If it is, the algorithm returns the index of the current character as the answer. If no such character is found, the algorithm returns -1 as the answer.

The time complexity of this algorithm is O(n), where n is the length of the input string 's'.