-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfirst_non_repeating_char.rb
53 lines (47 loc) · 1.35 KB
/
first_non_repeating_char.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Given a string, find its first non-repeating character
# Given a string, find the first non-repeating character in it.
# For example, if the input string is “GeeksforGeeks”, then the output should be ‘f’ and if the input string is “GeeksQuiz”, then the output should be ‘G’.
def first_non_repeating(str)
ar = Array.new(256, 0)
str.each_char { |c| ar[c.ord] += 1 }
str.each_char do |c|
return c if ar[c.ord] == 1
end
nil
end
def first_non_repeating_optimized(str)
ar = Array.new(256) { [0, -1] }
str.split('').each_with_index do |c, index|
ar[c.ord][0] += 1
ar[c.ord][1] = index if ar[c.ord][1] == -1
end
ar[0][1] = -11
first = nil
min_index = str.length
ar.each_with_index do |arr, index|
if arr[0] == 1 && arr[1] < min_index
first = index.chr
min_index = [min_index, arr[1]].min
end
end
first
end
# INT_MAX = (8*8)**2
def first_non_repeating_optimized2(str)
ar = Array.new(256) { -1 }
str.split('').each_with_index do |c, index|
ar[c.ord] = if ar[c.ord] == -1
index
else
-2
end
end
first = str.length
ar.each do |e|
first = [first, e].min if e >= 0
end
str[first]
end
puts first_non_repeating_optimized('GeeksforGeeks')
puts first_non_repeating_optimized('Geeks')
puts first_non_repeating_optimized('aman')