Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def constructSquare(s)
- letter_count = Array.new(26)
- x = 0
- while (x < 26)
- letter_count[x] = 0
- x = x + 1
- end
- x = 0
- while (x < s.length)
- letter_count[s[x].ord - 'a'.ord] = letter_count[s[x].ord - 'a'.ord] + 1
- x = x + 1
- end
- letter_count = letter_count.sort
- best = -1
- min_number = 10 ** (s.length - 1)
- x = Math.sqrt(min_number).to_i
- while (x <= 10 ** ((s.length + 1) / 2))
- n = x * x
- digit_count = Array.new(26)
- z = 0
- while (z < 26)
- digit_count[z] = 0
- z = z + 1
- end
- while (n > 0)
- digit_count[n % 10] = digit_count[n % 10] + 1
- n = n / 10
- end
- digit_count = digit_count.sort
- if letter_count == digit_count then
- best = x * x
- end
- x = x + 1
- end
- return best
- end
Advertisement
Add Comment
Please, Sign In to add comment