interviewbit_string_level_3
interviewbit_string_level_3
strings
Level 3
Justified Text
Problem Description
Given an array of words and a length of L, format the text such that each line has exactly L characters and is fully (left and right)
justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line.
Pad extra spaces ' ' when necessary so that each line has exactly L characters. Extra spaces between words should be distributed
as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be
assigned more spaces than the slots on the right. For the last line of text, it should be left justified and no extra space is inserted
between words.
Your program should return a list of strings, where each string represents a single line.
Note: Each word is guaranteed not to exceed L in length.
Problem Constraints
0 <= |A| <= 1000
0 <= B <= 5 * 104
Input Format
The first argument is an array of strings A representing words.
The second argument is an integer B representing L.
Output Format
Return a list of strings, where each string represents a single line.
Example Input
A: ["This", "is", "an", "example", "of", "text", "justification."]
B: 16.
Example Output
[ "This is an", "example of text", "justification. " ]
Example Explanation
Given words: ["This", "is", "an", "example", "of", "text", "justification."] L: 16.
Return the formatted lines as: [ "This is an", "example of text", "justification. " ]
def arrange_space(a, start_idx, end_idx, spaces): n = len(A)
word_cnt = end_idx-start_idx+1 ans = []
line = "" i=0
if word_cnt == 1: while i < n:
line += a[start_idx] word_count = 0
line += " "*spaces width = 0
return line start_idx = i
evenly_dist = spaces//(word_cnt-1) while i < n:
extra_spaces = spaces%(word_cnt-1) width += len(A[i])
for i in range(start_idx, end_idx+1): word_count += 1
line += a[i] if width+word_count-1 <= B:
if end_idx == len(a)-1 and spaces > 0: i += 1
line += " " else:
spaces -= 1 width -= len(A[i])
if i == end_idx: word_count -= 1
for i in range(spaces): break
line += " " end_idx = i-1
else: line = arrange_space(A, start_idx, end_idx, B-width)
if i != end_idx: ans.append(line)
for i in range(evenly_dist): return ans
line += " "
if extra_spaces > 0: if __name__ == "__main__":
line += " " A = input().split()
extra_spaces -= 1 B = int(input())
return line print(fullJustify(A,B))
Stringoholics
Problem Description
You are given an array A consisting of strings made up of the letters 'a' and 'b' only.
Each string goes through a number of operations, where:
1. At time 1, you circularly rotate each string by 1 letter. 2. At time 2, you circularly rotate the new rotated strings by 2 letters.
3. At time 3, you circularly rotate the new rotated strings by 3 letters. 4. At time i, you circularly rotate the new rotated strings
by i % length(string) letters.
Eg: String is "abaa"
1.At time 1, string is "baaa", as 1 letter is circularly rotated to the back
2.At time 2, string is "aaba", as 2 letters of the string "baaa" is circularly rotated to the back
3.At time 3, string is "aaab", as 3 letters of the string "aaba" is circularly rotated to the back
4.At time 4, string is again "aaab", as 4 letters of the string "aaab" is circularly rotated to the back
5. At time 5, string is "aaba", as 1 letters of the string "aaab" is circularly rotated to the back
After some units of time, a string becomes equal to its original self.
Once a string becomes equal to itself, it's letters start to rotate from the first letter again (process resets). So, if a string
takes t time to get back to the original, at time t+1 one letter will be rotated and the string will be its original self at 2t time.
You have to find the minimum time, where maximum number of strings are equal to their original self.
As this time can be very large, give the answer modulo 109+7.
Note: Your solution will run on multiple test cases so do clear global variables after using them.
Problem Constraints
1 <= |A| <= 100
Input Format
A: Array of strings.
Output Format
Minimum time, where maximum number of strings are equal to their original self.
Example Input
Input 1:A: [a, ababa, aba]
Input 2:A : [a, aa]
Example Output
Output 1:4
Output 2:1
Example Explanation
Explanation 1:String 'a' is it's original self at time 1, 2, 3 and 4. String 'ababa' is it's original self only at time 4. (ababa =>
babaa => baaba => babaa => ababa) String 'aba' is it's original self at time 2 and 4. (aba => baa => aba) Hence, 3 strings are
their original self at time 4.
Explanation 2: Both strings are their original self at time 1.
import math
from functools import reduce
N_MAX = 3000000
MOD = 1000000007
_mem_periods = {}
_mem_divisors = {}
def solve(A):
_mem_periods = {0: 1, 1: 1}
_mem_divisors = {1: [1]}
string_periods = [string_period(el) for el in A]
periods = [period(el) for el in string_periods]
return reduce(lcm, periods) % MOD
def period(x):
if x not in _mem_periods:
tmp = 0
for i in range(1, N_MAX + 1):
tmp += i
if tmp % x == 0:
_mem_periods[x] = i
break
return _mem_periods[x]
for f in divisors(n_s)[:-1]:
def divisors(x): n_copies = n_s//f
old_x = x if all(s[i_c*f + i] == s[(i_c + 1)*f + i]
if old_x not in _mem_divisors: for i_c in range(n_copies - 1) for i in
tmp = [1, x] range(f)):
while x > 3: return f
found_divisor = False
for i in range(2, int(x**0.5) + 1): return n_s
if x % i == 0:
tmp.extend([i, x//i]) def string_period_naive(s):
x //= i dummy = s + s
found_divisor = True n_s = len(s)
break for i in range(1, n_s + 1):
if not found_divisor: if dummy[i:i + n_s] == s:
break return i
def string_period(s):
n_s = len(s)
Text Editor
Problem Description
Michael is working on a very basic text editor. Initially, nothing is written on the editor. Also, the editor stores a buffer which is
initially empty. In one operation, Michael can do one of the following operations-
1. Append any lowercase English character to the current string on the editor. After this operation, the buffer becomes empty (if
anything was stored in the buffer previously).
2. Copy the current string on the editor to the buffer. Note that the complete string gets copied. You can't copy strings partially.
Any previously stored string in the buffer is replaced.
3. Append the string in the buffer to the current string. Note that after this operation, the buffer is still intact.
You are given a string A. Help Michael find the minimum number of operations required to write the string A on the editor.
Problem Constraints
1 <= |A| <= 5 x 105
Input Format
The first and only argument contains the string A.
Output Format
Return an integer, the minimum number of operations required to write the string A on the text editor.
Example Input
Input 1: A : "abababababab“
Input 2:A : "aaaabaaaab"
Example Output
Output 1: 7
Output 2:7
Example Explanation
Explanation 1: One of the ways to write this string in 7 operations is- 1. Append 'a' to the editor. Current string = "a",
Current Buffer = "" 2. Append 'b' to the editor. Current string = "ab", Current Buffer = "" 3. Copy the string to buffer.
Current string = "ab", Current Buffer = "ab" 4. Append the string in buffer to current string. Current string = "abab",
Current Buffer = "ab" 5. Append the string in buffer to current string. Current string = "ababab", Current Buffer = "ab"
6. Copy the string to buffer. Current string = "ababab", Current Buffer = "ababab" 7. Append the string in buffer to
current string. Current string = "abababababab", Current Buffer = "ababab" Explanation 2:
One of the ways to write this string in 7 operations is- 1. Append 'a' to the editor. Current string = "a", Current Buffer =
"" 2. Append 'a' to the editor. Current string = "aa", Current Buffer = "" 3. Copy the string to buffer. Current string =
"aa", Current Buffer = "aa" 4. Append the string in buffer to current string. Current string = "aaaa", Current Buffer =
"aa" 5. Append 'b' to the editor. Current string = "aaaab", Current Buffer = "" 6. Copy the string to buffer. Current string
= "aaaab", Current Buffer = "aaaab" 7. Append the string in buffer to current string. Current string = "aaaabaaaab",
Current Buffer = "aaaab"
N = 500005 sieve()
f = [[] for i in range(N)] s=A
ans = [0 for i in range(N)] pi = prefix_function(s)
def sieve(): n = len(s)
if len(f[1]): ans[0] = 1
return for i in range(1,n):
for i in range(1,N): ans[i] = 1 + ans[i-1]
for j in range(i,N,i): k = i+1 - pi[i]
f[j].append(i) if (i+1) % k:
k = i+1
def prefix_function(s): c = (i+1)//k
n = len(s) for j in f[c]:
pi = [0 for i in range(n)] if j == c:
for i in range(1, n): break
j = pi[i-1] ans[i] = min(ans[i], ans[k*j-1] + c//j)
while j > 0 and s[i] != s[j]: return ans[n-1]
j = pi[j-1]
if s[i] == s[j]: if __name__ == "__main__":
j += 1 A = input()
pi[i] = j print(solve(A))
return pi
def solve(A):
Compact Scientific Notation
Problem Description
You are presented with a positive decimal number, denoted as 'x.' Your objective is to convert it into "simple exponential notation."
In this notation, if x = a·10b, where 1 ≤ a < 10, the representation takes the form "aEb." If b equals zero, the "Eb" part should be omitted. When
'a' is an integer, it must be written without a decimal point. Additionally, there should be no extraneous zeroes in 'a' and 'b.'
Problem Constraints
The length of the number will not exceed 10 6.
Input Format
The first argument is the positive decimal number X.
Output Format
Return the "simple exponential notation" of the given number X.
Example Input
Input 1:.100
Input 2:16
Example Output
Output 1:1E-1
Output 2:1.6E1
Example Explanation
For Input 1:The given decimal number is 0.1. This is represented as "1E-1" in compact scientific notation since it can be expressed as 1
multiplied by 10 to the power of -1.
For Input 2:The provided decimal number is 16. In compact scientific notation, this is represented as "1.6E1" as it can be expressed as 1.6
multiplied by 10 to the power of 1.
def solve(s):
l = len(s) result = s[p]
k=l flag = 0
p=l-1
for i in range(p + 1, l):
while s[p] == '0': if s[i] == '.':
p -= 1 continue
l=p+1 elif flag == 0:
result += '.' + s[i]
for i in range(l): flag = 1
if s[i] == '.': else:
k=i result += s[i]
break
p=l-1 if b != 0:
while s[p] == '0' or s[p] == '.': result += 'E' + str(b)
p -= 1
l=p+1 return result
p=0
while s[p] == '0' or s[p] == '.': if __name__ == "__main__":
p += 1 A = input()
print(solve(A))
if p < k:
b=k-p-1
else: