Given a cable s, discover the first non-repeating personality in the andreturn the index. If it does no exist, return -1.

You are watching: First unique character in a string leetcode


Input: s = "leetcode" Output: 0EXAMPLE :2

Input: s = "loveleetcode" Output: 2I have been do the efforts this problem. I assumed we will pick one by one every the characters and check if a repeating character exists rest from the loop. And also if not then return that index.I have thought end a systems which I believe is no the most efficient means but I desire to understand the how have the right to I fix this problem with the technique given below:

public int firstUniqChar(String s) { for(int i=0;iI"m perplexed how come return the index.I"m can not to discover the reasonable after:

for(int j=i+1;jIf everyone can assist me discover out the reasonable here.

java cable for-loop if-statement
boost this concern
inquiry Aug 5 at 7:42

logo SharmaRohan Sharma
2944 bronze title
add a comment |

4 answer 4

active earliest Votes
Try this.

public revolution int firstUniqChar(String s) { L: because that (int i = 0, length = s.length(); i output:

enhance this answer
reply Aug 5 in ~ 8:22


14.5k22 gold badges1414 silver- badges3737 bronze title
include a comment |
you have the right to use a flag variable.

publicly int firstUniqChar(String s) { int flag=0; for(int i=0;i
enhance this answer
edited Aug 5 at 16:12
reply Aug 5 at 7:52


Shruti SinghalShruti Singhal
7611 silver- badge66 bronze badges
include a comment |

Fix friend code

You need to add a variable the tells girlfriend if you have actually breaked the loop or not

static int firstUniqChar(String s) { boolean duplicate; because that (int i = 0; i


There is a smarter way, that is recognize the last index occurence that the present char, if it"s equal to the existing index : the char is unique and also you return the index

static int firstUniqChar(String s) { for (int ns = 0; ns
boost this price
answer Aug 5 in ~ 7:51


39.7k77 gold badges2727 silver badges6262 bronze badges
add a comment |
There are 26 possible lowercase English letters, so you might use two 26 aspect arrays.

One array, letterCount, keeps counts of each letter. Begin at 0 and add 1 every time the equivalent letter shows up in the message string. The 2nd array, position, hold the place of the an initial occurrence of that letter, or -1 if the letter never ever appears. You will should initialise that variety to -1 for every elements.

Process the cable in order, record initial positions, as soon as only for each letter, and also incrementing the count for each letter in the string.

After the string has actually been processed, look with the letterCount array. If there room no letters with a 1 count then return -1. If precisely one letter has actually a 1 count, climate return the position of the letter from the place array. If much more than one letter has actually a 1 count, then pick the one through the lowest value for the position.

See more: Skyrim Se Dark Brotherhood Armor Mod S For Skyrim, Dark Brotherhood/Assassin Armor Mods: Skyrimmods

Using 2 loops is a very inefficient means of addressing this problem. The string have the right to be as much as 100,000 personalities long and also you are processing it lot of times. Far much better to process it just once, keeping track the what friend have uncovered so far.