Skip to main content

First Unique Character in a String


Problem


Find the first non-repeating character in a given String and return it's index. If it doesn't exist, return -1.


Solution

We can execute the logic in two steps
  1. Find the frequencies of every letter how many times they appear in the string
  2. Using those frequencies find the the first letter which has value as 1

We need to use a Map to keep track of the first index of a letter and separate storage to maintain the frequency count.

Example code is as below:

Comments

  1. Slot Search - Las Vegas, NV - Mapyro
    Las Vegas, NV 동두천 출장샵 - Slots 동두천 출장샵 Search. 서산 출장안마 Las Vegas, NV. 777 Casino Drive. Las 충주 출장안마 Vegas, NV 89109, USA. 777 Casino Drive. Mapyro. 군포 출장마사지

    ReplyDelete

Post a Comment

Popular posts from this blog

Number Complement

Number Complement For Positive Integer For a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Solution  To solve above problem we need to generate the binary representation of a number and go through the bits to find the complement of the bit.  To get the complement a simple logic can we do the XOR the bit with 1 which will give its complement  i.e.  1 ^ 1 = 0 0 ^ 1 = 1 Sample Code  

Find the Town Judge

Problem In a town, there are 'n' people labelled from 1 to 'n'. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. There is exactly one person that satisfies properties 1 and 2. The given input is an array of pairs trust[i] = [a, b] representing that the person labelled 'a' trusts the person labelled 'b'. If the town judge exists and can be identified, return the label of the town judge.  Otherwise, return '-1'. Solution To solve the problem we can use a simple array of size N+1 (we will use N+1 to avoid maintaining numbers as arrays starts with index 0 and we have people numbered from 1). Every index starting one represent a member of the town. We can use following logic to solve the problem. Lets go through the entire list of trust values and do following things For every &#