jonoson
BAN USERIn Ruby:
Assume there are 3 question marks, use binary string from 000 to 111. Convert it to string, replace binary chars with each question marks.
# s = "a?bc?def?g", 3 question marks
def fill_qmarks_iterate( s )
count_q = s.chars.count("?")
n = 2 ** count_q # n = 1000 to 1111
for x in n..n*2-1
s2 = s.clone
# from 000 to 111
x.to_s(2).chars[1..s.chars.count("?")].each do |y|
s2[ s2.index("?") ] = y # replace with first ? from left
end
p s2
end
end
In Ruby code, O(N)
def validate_hire( str )
alphabet = "hire".split("")
i = -1
prev_x = nil
str.split("").each do |x|
i += 1 unless prev_x == x
return false if alphabet[i % alphabet.size] != x
prev_x = x
end
(i+1) % alphabet.size == 0
end
def main()
p validate_hire("hire") # ==> true
p validate_hire("hhiirree") # ==> true
p validate_hire("hhiiiiirrreeeehire") # ==> true
p validate_hire("hireh") # ==> false
end
RepHenryBrown, Associate at A9
I am an experienced budget analyst skilled at researching and consolidating financial and budget information. I am taught record keeping ...
RepCliftonMalone, Android Engineer at ABC TECH SUPPORT
Hello, I am a Seo Analyst with 5 years of experience in helping large ecommerce websites reach higher organic positions ...
RepGarzaHodge, Backend Developer at Apache Design
I am Garza, and I work in many different research, education, and health care settings with varying roles, levels of ...
RepAriasBitner, Systems developer at 247quickbookshelp
I am a Systems developer . My duties primarily revolve around building software by writing code, as well as modifying software ...
RepLucindaBriella, Accountant at Clean Power Research
Lucinda, I conduct the admissions process and promote school to prospective students and their guardians. I need a spell caster ...
In Ruby,
Using binary search to find the last positions of each kind of elements, and calculate count.
Using a heap to store all kinds of element and find the max.
Complexity: O(klogn), k is the number of kinds of elements.
In the example, [1,1,2,2,2,2,3,3,3], k will be 3
I'm using array in Ruby to mimic heap.
- jonoson August 11, 2014