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

EXAMPLE : 1

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
share
boost this concern
follow
inquiry Aug 5 at 7:42
*

logo SharmaRohan Sharma
2944 bronze title
add a comment |

4 answer 4


active earliest Votes
1
Try this.

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

02-1
re-publishing
enhance this answer
monitor
reply Aug 5 in ~ 8:22

*

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

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

*

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

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

Improve

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
share
boost this price
follow
answer Aug 5 in ~ 7:51

*

azroazro
39.7k77 gold badges2727 silver badges6262 bronze badges
2
add a comment |
0
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.