Skip to content

Commit 1382976

Browse files
committed
leetcode
1 parent 506ad2d commit 1382976

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
3+
-* 387. First Unique Character in a String *-
4+
5+
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
6+
7+
8+
9+
Example 1:
10+
11+
Input: s = "leetcode"
12+
Output: 0
13+
Example 2:
14+
15+
Input: s = "loveleetcode"
16+
Output: 2
17+
Example 3:
18+
19+
Input: s = "aabb"
20+
Output: -1
21+
22+
23+
Constraints:
24+
25+
1 <= s.length <= 105
26+
s consists of only lowercase English letters.
27+
28+
29+
30+
31+
32+
33+
34+
*/
35+
36+
import 'dart:collection';
37+
38+
class Solution {
39+
int firstUniqChar(String s) {
40+
// hashmap to store the unique values
41+
HashMap<String, int?> map = HashMap();
42+
// looping through each and every element in the String
43+
for (int i = 0; i < s.length; i++) {
44+
// assigning the int as index for every element
45+
map[s[i]] = (map[s[i]] ?? 0) + 1;
46+
}
47+
// looping again
48+
for (int i = 0; i < s.length; i++) {
49+
// if the element matches with index value as saved above
50+
if (map[s[i]] == 1) {
51+
// than we will return that element
52+
return i;
53+
}
54+
}
55+
// sentinel value
56+
return -1;
57+
}
58+
}
59+
60+
class B {
61+
int firstUniqChar(String s) {
62+
// index as ans
63+
int ans = 0;
64+
// into array list of each and every character
65+
List<String> chars = s.split("");
66+
// if that character is inside the array of character
67+
for (String char in chars) {
68+
// comparing first to last index
69+
if (s.indexOf(char) == s.lastIndexOf(char)) {
70+
// assigning the index to our value
71+
return ans = s.indexOf(char);
72+
} else {
73+
ans = -1;
74+
}
75+
}
76+
return ans;
77+
}
78+
}
79+
80+
class C {
81+
int firstUniqChar(String s) {
82+
// frequency of array based on the length of alphabets inn ENGLISH
83+
List<int> freq = List.filled(26, 0);
84+
// looping through the length of string
85+
for (int i = 0; i < s.length; i++)
86+
// getting the index of the each individual string character
87+
freq[s.codeUnitAt(i) - 'a'.codeUnitAt(0)]++;
88+
for (int i = 0; i < s.length; i++)
89+
// if it matches with index we will return
90+
if (freq[s.codeUnitAt(i) - 'a'.codeUnitAt(0)] == 1) return i;
91+
return -1;
92+
}
93+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
func firstUniqChar(s string) int {
4+
flags := make([]int, 26)
5+
for i := range flags {
6+
flags[i] = -1
7+
}
8+
slen := len(s)
9+
10+
for i, ch := range s {
11+
idx := byte(ch - 'a')
12+
if flags[idx] == -1 {
13+
flags[idx] = i
14+
} else {
15+
flags[idx] = slen
16+
}
17+
}
18+
19+
min := slen
20+
for i := range flags {
21+
if flags[i] >= 0 && flags[i] < len(s) && flags[i] < min {
22+
min = flags[i]
23+
}
24+
}
25+
26+
if min == slen {
27+
return -1
28+
}
29+
return min
30+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 🔥 First Unique Character in a String 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 HashMap
4+
5+
```dart
6+
class Solution {
7+
int firstUniqChar(String s) {
8+
// hashmap to store the unique values
9+
HashMap<String, int?> map = HashMap();
10+
// looping through each and every element in the String
11+
for (int i = 0; i < s.length; i++) {
12+
// assigning the int as index for every element
13+
map[s[i]] = (map[s[i]] ?? 0) + 1;
14+
}
15+
// looping again
16+
for (int i = 0; i < s.length; i++) {
17+
// if the element matches with index value as saved above
18+
if (map[s[i]] == 1) {
19+
// than we will return that element
20+
return i;
21+
}
22+
}
23+
// sentinel value
24+
return -1;
25+
}
26+
}
27+
```
28+
29+
## Solution - 2 Frequency Array
30+
31+
```dart
32+
class Solution {
33+
int firstUniqChar(String s) {
34+
// index as ans
35+
int ans = 0;
36+
// into array list of each and every character
37+
List<String> chars = s.split("");
38+
// if that character is inside the array of character
39+
for (String char in chars) {
40+
// comparing first to last index
41+
if (s.indexOf(char) == s.lastIndexOf(char)) {
42+
// assigning the index to our value
43+
return ans = s.indexOf(char);
44+
} else {
45+
ans = -1;
46+
}
47+
}
48+
return ans;
49+
}
50+
}
51+
52+
```
53+
54+
## Solution - 3
55+
56+
```dart
57+
class Solution {
58+
int firstUniqChar(String s) {
59+
// frequency of array based on the length of alphabets inn ENGLISH
60+
List<int> freq = List.filled(26, 0);
61+
// looping through the length of string
62+
for (int i = 0; i < s.length; i++)
63+
// getting the index of the each individual string character
64+
freq[s.codeUnitAt(i) - 'a'.codeUnitAt(0)]++;
65+
for (int i = 0; i < s.length; i++)
66+
// if it matches with index we will return
67+
if (freq[s.codeUnitAt(i) - 'a'.codeUnitAt(0)] == 1) return i;
68+
return -1;
69+
}
70+
}
71+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
138138
- [**367.** Valid Perfect Square](ValidPerfectSquare/valid_perfect_square.dart)
139139
- [**383.** Ransom Note](RansomNote/ransom_note.dart)
140140
- [**587.** Erect the Fence](ErectTheFence/erect_the_fence.dart)
141+
- [**387.** First Unique Character in a String](FirstUniqueCharacterInAString/first_unique_character_in_a_string.dart)
141142

142143
## Reach me via
143144

0 commit comments

Comments
 (0)