Skip to main content

Command Palette

Search for a command to run...

First Unique Character in a String

Published
1 min read
First Unique Character in a String
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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

LeetCode Problem - 387

class Solution {
    // This method finds the index of the first unique character in a string.
    public int firstUniqChar(String s) {
        // Convert the string to a character array.
        char[] c = s.toCharArray();

        // Iterate through the characters of the string.
        for (int i = 0; i < s.length(); i++) {
            // Initialize a count variable to count occurrences of the current character.
            int count = 0;
            // Iterate through the characters of the string again to compare with the current character.
            for (char currentChar : c) {
                // If the current character matches the character at index i, increment the count.
                if (currentChar == (s.charAt(i))) {
                    count++;
                }
                // If the count exceeds 1, break the loop as the character is not unique.
                if (count > 1) {
                    break;
                }
            }
            // If the count is 1, return the index of the first unique character.
            if (count == 1) {
                return i;
            }
        }
        // If no unique character is found, return -1.
        return -1;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.