description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers.
Example 1:
Input:
S1 = 3, S2 = 3
ValueS1 = {2,3,4}
ValueS2 = {3,4,5}
Output: 5 7 9
Explanation: After adding the 2 numbers
the resultant number is 5 7 9.
Example 2:
Input:
S1 = 1, S2 = 2
ValueS1 = {9}
ValueS2 = {8,7}
Output: 9 6
Explanation: Add 9 and 7 we get 16.
1 is carry here and is added to 8.
So the answer is 9 6
Your Task:
The task is to complete the function addSameSize() addCarryToRemaining().
Constraints:
1 <= S1, S2 <= 100 | def addSameSize(h1, h2, carry):
h1 = reverseList(h1)
h2 = reverseList(h2)
head = h1
while h1 != None:
value = h1.data + h2.data + carry.data
carry.data = value // 10
h1.data = value % 10
h1 = h1.next
h2 = h2.next
head = reverseList(head)
return head
def addCarryToRemaining(h1, curr, result, carry):
if carry.data != 0:
h1 = reverseList(h1)
curr = curr.next
while curr != None:
value = curr.data + carry.data
carry.data = value // 10
curr.data = value % 10
curr = curr.next
h1 = reverseList(h1)
result.head = h1
def reverseList(head):
curr = head
prev = None
while curr != None:
temp = curr.next
curr.next = prev
prev = curr
curr = temp
return prev | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers.
Example 1:
Input:
S1 = 3, S2 = 3
ValueS1 = {2,3,4}
ValueS2 = {3,4,5}
Output: 5 7 9
Explanation: After adding the 2 numbers
the resultant number is 5 7 9.
Example 2:
Input:
S1 = 1, S2 = 2
ValueS1 = {9}
ValueS2 = {8,7}
Output: 9 6
Explanation: Add 9 and 7 we get 16.
1 is carry here and is added to 8.
So the answer is 9 6
Your Task:
The task is to complete the function addSameSize() addCarryToRemaining().
Constraints:
1 <= S1, S2 <= 100 | def addSameSize(h1, h2, carry):
def reverse(head):
prev = None
curr = head
next1 = None
while curr != None:
next1 = curr.next
curr.next = prev
prev = curr
curr = next1
return prev
h1 = reverse(h1)
h2 = reverse(h2)
n = h1
m = h2
p = None
headp = None
while n != None and m != None:
value = n.data + m.data + carry.data
incre = value % 10
carry.data = value // 10
if p == None:
p = n
p.data = incre
headp = p
else:
p.next = n
p = p.next
p.data = incre
n = n.next
m = m.next
headp = reverse(headp)
return headp
def addCarryToRemaining(h1, curr, result, carry):
def reverse(head):
prev = None
curr = head
next1 = None
while curr != None:
next1 = curr.next
curr.next = prev
prev = curr
curr = next1
return prev
n = h1
while n.next != curr:
n = n.next
n.next = None
h1 = reverse(h1)
p = h1
temp = None
while p != None:
value = p.data + carry.data
incre = value % 10
carry.data = value // 10
p.data = incre
p = p.next
temp = h1
h1 = reverse(h1)
p = h1
h1 = p
headp = p
while p.next != None:
p = p.next
p.next = curr
p = p.next
result.head = headp | FUNC_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR NONE VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR |
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers.
Example 1:
Input:
S1 = 3, S2 = 3
ValueS1 = {2,3,4}
ValueS2 = {3,4,5}
Output: 5 7 9
Explanation: After adding the 2 numbers
the resultant number is 5 7 9.
Example 2:
Input:
S1 = 1, S2 = 2
ValueS1 = {9}
ValueS2 = {8,7}
Output: 9 6
Explanation: Add 9 and 7 we get 16.
1 is carry here and is added to 8.
So the answer is 9 6
Your Task:
The task is to complete the function addSameSize() addCarryToRemaining().
Constraints:
1 <= S1, S2 <= 100 | def addSameSize(h1, h2, carry):
num1 = 0
num2 = 0
while h1:
num1 = num1 * 10 + h1.data
h1 = h1.next
while h2:
num2 = num2 * 10 + h2.data
h2 = h2.next
res = num1 + num2
dummy = Node(res % 10)
res //= 10
while res != 0:
num = res % 10
p = Node(num)
p.next = dummy
dummy = p
res //= 10
return dummy
def addCarryToRemaining(h1, curr, result, carry):
count = 0
currptr = curr
while currptr:
count += 1
currptr = currptr.next
res = result.head
count1 = 0
while res:
count1 += 1
res = res.next
temp = h1
num1 = 0
while temp:
num1 = num1 * 10 + temp.data
if temp.next == curr:
break
temp = temp.next
if count == count1:
temp.next = result.head
result.head = h1
return result.head
else:
fn_res = num1 + 1
result.head = result.head.next
while fn_res != 0:
num = fn_res % 10
p = Node(num)
p.next = result.head
result.head = p
fn_res //= 10
return result.head | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers.
Example 1:
Input:
S1 = 3, S2 = 3
ValueS1 = {2,3,4}
ValueS2 = {3,4,5}
Output: 5 7 9
Explanation: After adding the 2 numbers
the resultant number is 5 7 9.
Example 2:
Input:
S1 = 1, S2 = 2
ValueS1 = {9}
ValueS2 = {8,7}
Output: 9 6
Explanation: Add 9 and 7 we get 16.
1 is carry here and is added to 8.
So the answer is 9 6
Your Task:
The task is to complete the function addSameSize() addCarryToRemaining().
Constraints:
1 <= S1, S2 <= 100 | def addSameSize(h1, h2, carry):
if not h1:
return
sm = 0
result = Node(sm)
result.next = addSameSize(h1.next, h2.next, carry)
sm = h1.data + h2.data + carry.data
carry.data = sm // 10
sm = sm % 10
result.data = sm
return result
def addCarryToRemaining(h1, curr, result, carry):
if h1 != curr:
addCarryToRemaining(h1.next, curr, result, carry)
sm = h1.data + carry.data
carry.data = sm // 10
sm = sm % 10
result.head = push(result.head, sm)
return | FUNC_DEF IF VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN |
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers.
Example 1:
Input:
S1 = 3, S2 = 3
ValueS1 = {2,3,4}
ValueS2 = {3,4,5}
Output: 5 7 9
Explanation: After adding the 2 numbers
the resultant number is 5 7 9.
Example 2:
Input:
S1 = 1, S2 = 2
ValueS1 = {9}
ValueS2 = {8,7}
Output: 9 6
Explanation: Add 9 and 7 we get 16.
1 is carry here and is added to 8.
So the answer is 9 6
Your Task:
The task is to complete the function addSameSize() addCarryToRemaining().
Constraints:
1 <= S1, S2 <= 100 | def addSameSize(h1, h2, carry):
def addList1(h1, h2, c):
if h1.next == None:
val = h2.data + h1.data + c
h1.data = val % 10
return val // 10
c = addList1(h1.next, h2.next, 0)
val = h1.data + h2.data + c
h1.data = val % 10
return val // 10
cr = addList1(h1, h2, 0)
carry.data = cr
return h1
def addCarryToRemaining(h1, curr, result, carry):
def addList2(h1, curr, carry):
if h1.next == curr:
val = h1.data + carry.data
h1.data = val % 10
carry.data = val // 10
return
addList2(h1.next, curr, carry)
val = h1.data + carry.data
h1.data = val % 10
carry.data = val // 10
return carry.data
addList2(h1, curr, carry)
result.head = h1 | FUNC_DEF FUNC_DEF IF VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR |
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers.
Example 1:
Input:
S1 = 3, S2 = 3
ValueS1 = {2,3,4}
ValueS2 = {3,4,5}
Output: 5 7 9
Explanation: After adding the 2 numbers
the resultant number is 5 7 9.
Example 2:
Input:
S1 = 1, S2 = 2
ValueS1 = {9}
ValueS2 = {8,7}
Output: 9 6
Explanation: Add 9 and 7 we get 16.
1 is carry here and is added to 8.
So the answer is 9 6
Your Task:
The task is to complete the function addSameSize() addCarryToRemaining().
Constraints:
1 <= S1, S2 <= 100 | def reverse(h):
if h == None or h.next == None:
return h
curr = h
next = h.next
prev = None
while next:
curr.next = prev
prev = curr
curr = next
next = curr.next
curr.next = prev
return curr
def addSameSize(h1, h2, carry):
h1 = reverse(h1)
h2 = reverse(h2)
res = Node(0)
p = res
while h1 and h2:
sum = h1.data + h2.data + carry.data
carry.data = sum // 10
p.next = Node(sum % 10)
p = p.next
h1 = h1.next
h2 = h2.next
res = reverse(res.next)
return res
def addCarryToRemaining(h1, curr, result, carry):
h1 = reverse(h1)
curr = curr.next
res = curr
while curr:
curr.data += carry.data
carry.data = curr.data // 10
curr.data = curr.data % 10
curr = curr.next
res = reverse(res)
p = res
while p.next:
p = p.next
p.next = result.head
result.head = res | FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR |
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers.
Example 1:
Input:
S1 = 3, S2 = 3
ValueS1 = {2,3,4}
ValueS2 = {3,4,5}
Output: 5 7 9
Explanation: After adding the 2 numbers
the resultant number is 5 7 9.
Example 2:
Input:
S1 = 1, S2 = 2
ValueS1 = {9}
ValueS2 = {8,7}
Output: 9 6
Explanation: Add 9 and 7 we get 16.
1 is carry here and is added to 8.
So the answer is 9 6
Your Task:
The task is to complete the function addSameSize() addCarryToRemaining().
Constraints:
1 <= S1, S2 <= 100 | def addSameSize(h1, h2, carry):
sm = h1.data + h2.data
res = None
if h1.next:
res = addSameSize(h1.next, h2.next, carry)
sm += carry.data
carry.data = sm // 10
x = Node(sm % 10)
x.next = res
return x
def addCarryToRemaining(head, curr, result, carry):
if head.next != curr:
addCarryToRemaining(head.next, curr, result, carry)
sm = head.data + carry.data
carry.data = sm // 10
x = Node(sm % 10)
x.next = result.head
result.head = x | FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR |
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers.
Example 1:
Input:
S1 = 3, S2 = 3
ValueS1 = {2,3,4}
ValueS2 = {3,4,5}
Output: 5 7 9
Explanation: After adding the 2 numbers
the resultant number is 5 7 9.
Example 2:
Input:
S1 = 1, S2 = 2
ValueS1 = {9}
ValueS2 = {8,7}
Output: 9 6
Explanation: Add 9 and 7 we get 16.
1 is carry here and is added to 8.
So the answer is 9 6
Your Task:
The task is to complete the function addSameSize() addCarryToRemaining().
Constraints:
1 <= S1, S2 <= 100 | def addSameSize(h1, h2, carry):
if h2 is None and h1 is None:
return
new_node = Node(0)
if h2 is not None and h1 is not None:
new_node.next = addSameSize(h1.next, h2.next, carry)
elif h2 is None and h1 is not None:
new_node.next = addSameSize(h1.next, h2, carry)
result = (h1.data + h2.data + carry.data) % 10
new_node.data = result
carry.data = (h1.data + h2.data + carry.data) // 10
return new_node
def addCarryToRemaining(h1, curr, result, carry):
if h1 != curr:
addCarryToRemaining(h1.next, curr, result, carry)
sm = h1.data + carry.data
carry.data = sm // 10
sm = sm % 10
result.head = push(result.head, sm)
return | FUNC_DEF IF VAR NONE VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN |
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers.
Example 1:
Input:
S1 = 3, S2 = 3
ValueS1 = {2,3,4}
ValueS2 = {3,4,5}
Output: 5 7 9
Explanation: After adding the 2 numbers
the resultant number is 5 7 9.
Example 2:
Input:
S1 = 1, S2 = 2
ValueS1 = {9}
ValueS2 = {8,7}
Output: 9 6
Explanation: Add 9 and 7 we get 16.
1 is carry here and is added to 8.
So the answer is 9 6
Your Task:
The task is to complete the function addSameSize() addCarryToRemaining().
Constraints:
1 <= S1, S2 <= 100 | def reverse(head):
curr = head
prev = None
while curr:
next1 = curr.next
curr.next = prev
prev = curr
curr = next1
return prev
def addSameSize(h1, h2, carry):
if not h1 or not h2:
return
if not h1.next or not h2.next:
if h1.data + h2.data + carry.data > 9:
h1.data = int(str(h1.data + h2.data + carry.data)[1])
carry.data = 1
else:
h1.data = h1.data + h2.data + carry.data
carry.data = 0
return h1
addSameSize(h1.next, h2.next, carry)
if h1.data + h2.data + carry.data > 9:
h1.data = int(str(h1.data + h2.data + carry.data)[1])
carry.data = 1
else:
h1.data = h1.data + h2.data + carry.data
carry.data = 0
return h1
def addCarryToRemaining(h1, curr, result, carry):
if not carry.data:
result.head = h1
return
prev = h1
chck = h1.next
while chck != curr:
prev = chck
chck = chck.next
prev.next = None
met = abc = reverse(h1)
while abc and carry.data:
if abc.data + carry.data > 9:
abc.data = int(str(abc.data + carry.data)[1])
carry.data = 1
else:
abc.data += carry.data
carry.data = 0
if not abc.next and carry.data:
break
abc = abc.next
if carry.data:
new = Node(carry.data)
carry.data = 0
abc.next = new
res = reverse(met)
met.next = chck
result.head = res
return | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN IF VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR FUNC_DEF IF VAR ASSIGN VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN |
Given two numbers represented by two linked lists, write a function that returns Sum list. The sum list is linked list representation of addition of two input numbers.
Example 1:
Input:
S1 = 3, S2 = 3
ValueS1 = {2,3,4}
ValueS2 = {3,4,5}
Output: 5 7 9
Explanation: After adding the 2 numbers
the resultant number is 5 7 9.
Example 2:
Input:
S1 = 1, S2 = 2
ValueS1 = {9}
ValueS2 = {8,7}
Output: 9 6
Explanation: Add 9 and 7 we get 16.
1 is carry here and is added to 8.
So the answer is 9 6
Your Task:
The task is to complete the function addSameSize() addCarryToRemaining().
Constraints:
1 <= S1, S2 <= 100 | def addSameSize(h1, h2, carry):
if h1.next == None and h2.next == None:
d = h1.data + h2.data
if d >= 10:
d -= 10
carry.data = 1
else:
carry.data = 0
return Node(d)
c = carry_data()
temp = addSameSize(h1.next, h2.next, c)
d = h1.data + h2.data + c.data
if d >= 10:
d -= 10
carry.data = 1
else:
carry.data = 0
ans = Node(d)
ans.next = temp
return ans
def addCarryToRemaining(h1, curr, result, carry):
all = []
while h1.next != curr:
all.append(h1)
h1 = h1.next
all.append(h1)
for i in range(len(all) - 1, -1, -1):
all[i].data += carry.data
if all[i].data == 10:
all[i].data = 0
else:
carry.data = 0
break
h1.next = result.head
result.head = all[0] | FUNC_DEF IF VAR NONE VAR NONE ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, S1: str, S2: str):
d = {}
def solve(s1, s2):
if s1 == s2:
d[s1 + "#" + s2] = True
return True
if len(s1) == 1:
d[s1 + "#" + s2] = False
return False
if s1 + "#" + s2 in d:
return d[s1 + "#" + s2]
for i in range(1, len(s1)):
if (
solve(s1[:i], s2[:i])
and solve(s1[i:], s2[i:])
or solve(s1[:i], s2[-i:])
and solve(s1[i:], s2[:-i])
):
d[s1 + "#" + s2] = True
return True
d[s1 + "#" + s2] = False
return False
if len(S1) != len(S2):
return False
return solve(S1, S2) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR NUMBER RETURN NUMBER IF BIN_OP BIN_OP VAR STRING VAR VAR RETURN VAR BIN_OP BIN_OP VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def __init__(self):
self.hmap = {}
def isScramble(self, S1: str, S2: str):
return self.helper(S1, S2)
def helper(self, s, t):
if (s, t) in self.hmap:
return self.hmap[s, t]
n = len(s)
if s == t:
self.hmap[s, t] = True
return True
cnt = [0] * 26
for i in range(0, n):
cnt[ord(s[i]) - ord("a")] += 1
cnt[ord(t[i]) - ord("a")] -= 1
for i in range(0, 26):
if cnt[i] != 0:
self.hmap[s, t] = False
return False
for x in range(1, n):
if self.helper(s[0:x], t[0:x]) and self.helper(s[x:], t[x:]):
self.hmap[s, t] = True
return True
if self.helper(s[0:x], t[n - x :]) and self.helper(s[x:], t[: n - x]):
self.hmap[s, t] = True
return True
self.hmap[s, t] = False
return False | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, S1: str, S2: str):
dic = dict()
def sol(S1, S2, i, j, n, m):
if j - i != m - n:
return 0
if j - i == 1 and S1[i] == S2[n]:
return 1
s = S1[i:j] + "_" + S2[n:m]
if s in dic:
return dic[s]
for k in range(1, j - i):
if (
sol(S1, S2, i, i + k, n, n + k)
and sol(S1, S2, i + k, j, n + k, m)
or sol(S1, S2, i, i + k, m - k, m)
and sol(S1, S2, i + k, j, n, m - k)
):
dic[s] = 1
return dic[s]
dic[s] = 0
return dic[s]
return sol(S1, S2, 0, len(S1), 0, len(S2)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR VAR BIN_OP VAR VAR RETURN NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR STRING VAR VAR VAR IF VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, S1: str, S2: str, d={}):
s1 = S1
s2 = S2
n1 = len(s1)
n2 = len(s2)
if n1 != n2:
return False
if n1 == 0:
return True
if s1 == s2:
return True
if n1 <= 1:
return False
key = s1 + s2
if key in d:
return d[key]
flag = False
for i in range(1, n1):
C1 = self.isScramble(s1[:i], s2[n1 - i :], d) and self.isScramble(
s1[i:], s2[: n1 - i], d
)
C2 = self.isScramble(s1[:i], s2[:i], d) and self.isScramble(
s1[i:], s2[i:], d
)
if C1 or C2:
flag = True
break
d[key] = flag
return flag | CLASS_DEF FUNC_DEF VAR VAR DICT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, s1: str, s2: str):
if len(s1) != len(s2):
return False
if s1 == s2:
return True
dp = {}
def solve(s1, s2):
n = len(s1)
if s1 == s2:
return True
if s1 + "" + s2 in dp:
return dp[s1 + "" + s2]
dp[s1 + "" + s2] = False
for i in range(1, n):
solve1 = solve(s1[:i], s2[:i]) and solve(s1[i:], s2[i:])
solve2 = solve(s1[:i], s2[n - i :]) and solve(s1[i:], s2[: n - i])
if solve1 or solve2:
dp[s1 + "" + s2] = True
return dp[s1 + "" + s2]
return solve(s1, s2) | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR DICT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR STRING VAR VAR RETURN VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR NUMBER RETURN VAR BIN_OP BIN_OP VAR STRING VAR RETURN FUNC_CALL VAR VAR VAR |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | dic = {}
class Solution:
def isScramble(self, S1: str, S2: str):
if len(S1) == 0:
return True
if S1 == S2:
return True
if S1 + " " + S2 in dic:
return dic[S1 + " " + S2]
flag = False
for i in range(1, len(S1)):
if self.isScramble(S1[:i], S2[:i]) and self.isScramble(S1[i:], S2[i:]):
flag = True
break
if self.isScramble(S1[:i], S2[-i:]) and self.isScramble(S1[i:], S2[:-i]):
flag = True
break
dic[S1 + " " + S2] = flag
return flag | ASSIGN VAR DICT CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR STRING VAR VAR RETURN VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR VAR RETURN VAR |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, S1: str, S2: str):
mp = {}
def solve(a, b):
if a == b:
return True
if len(a) <= 1:
return False
key = a + " " + b
if mp.get(key) != None:
return mp[key]
n = len(a)
flag = False
for k in range(1, n):
cond1 = solve(a[0:k], b[n - k :]) and solve(a[k:], b[0 : n - k])
cond2 = solve(a[0:k], b[0:k]) and solve(a[k:], b[k:])
if cond1 or cond2:
flag = True
break
mp[key] = flag
return flag
return solve(S1, S2) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR IF FUNC_CALL VAR VAR NONE RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def __init__(self):
self.scrambles = {}
def isScramble(self, s1: str, s2: str) -> bool:
if (s1, s2) in self.scrambles:
return self.scrambles[s1, s2]
if s1 == s2:
self.scrambles[s1, s2] = True
return True
ls = len(s1)
if ls == 1 or sorted(s1) != sorted(s2):
self.scrambles[s1, s2] = False
return False
for i in range(1, ls):
s1_left, s1_right = s1[:i], s1[i:]
s2_left, s2_right = s2[:i], s2[i:]
match1 = self.isScramble(s1_left, s2_left) and self.isScramble(
s1_right, s2_right
)
s2_left2, s2_right2 = s2[: ls - i], s2[ls - i :]
match2 = self.isScramble(s1_left, s2_right2) and self.isScramble(
s1_right, s2_left2
)
if match1 or match2:
self.scrambles[s1, s2] = True
return True
self.scrambles[s1, s2] = False | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER VAR |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, S1: str, S2: str):
if S1 == "" or S2 == "":
return True
elif S1 == S2 or S1 == S2[::-1]:
return True
i = 1
while i < len(S1):
if self.match(S1[:i], S2[-i:]) and self.match(S1[i:], S2[:-i]):
if S1[i:] + S1[:i] == S2:
return True
elif self.isScramble(S1[:i], S2[-i:]) and self.isScramble(
S1[i:], S2[:-i]
):
return True
return False
elif self.match(S1[:i], S2[:i]) and self.match(S1[i:], S2[i:]):
if self.isScramble(S1[:i], S2[:i]) and self.isScramble(S1[i:], S2[i:]):
return True
else:
return False
i += 1
def match(self, s1, s2):
s1 = list(s1)
s2 = list(s2)
s1.sort()
s2.sort()
if s1 != s2:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR IF VAR STRING VAR STRING RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, S1, S2):
self.leftRight = [0, len(S1) - 1]
self.string1 = S1
self.string2 = S2
self.tuples = [(0, len(S1) - 1)]
self.failedCases = []
for i in range(2):
Return = self.switch(self.leftRight[i], i)
if Return:
return True
return False
def switch(self, idx, drtn=0):
mainTuple, mainIdx = self.findTup(idx)
locations = self.getLocations(idx, mainTuple)
string2 = self.string2
if locations:
if drtn == 0:
self.leftRight[0] += 1
else:
self.leftRight[1] -= 1
if self.leftRight[0] == self.leftRight[1]:
return True
for index in locations:
changed = 0
if index > mainTuple[0]:
changed += 1
if drtn:
self.tuples.append((mainTuple[0], index - 1))
else:
self.tuples.append((mainTuple[0] + 1, index))
if index < mainTuple[1]:
changed += 1
if drtn:
self.tuples.append((index, mainTuple[1] - 1))
else:
self.tuples.append((index + 1, mainTuple[1]))
if not drtn:
self.string2 = (
string2[:idx]
+ string2[index]
+ string2[idx:index]
+ string2[index + 1 :]
)
else:
self.string2 = (
string2[:index]
+ string2[index + 1 : idx + 1]
+ string2[index]
+ string2[idx + 1 :]
)
if self.string2 not in self.failedCases:
for i in range(2):
Result = self.switch(self.leftRight[i], i)
if Result:
return True
for j in range(changed):
self.tuples.pop()
if drtn == 0:
self.leftRight[0] -= 1
else:
self.leftRight[1] += 1
else:
self.failedCases.append(self.string2)
self.string2 = string2
self.tuples.insert(mainIdx, mainTuple)
return False
def findTup(self, idx):
for i in range(len(self.tuples)):
tup = self.tuples[i]
if idx >= tup[0] and idx <= tup[1]:
self.tuples.pop(i)
return tup, i
def getLocations(self, idx, tup):
locations = []
for i in range(tup[0], tup[1] + 1):
if self.string1[idx] == self.string2[i]:
locations.append(i)
return locations | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def __init__(self):
self.mp = {}
def isScramble(self, s1: str, s2: str):
if s1 == s2:
return True
if len(s1) != len(s2):
return False
if s1 + " " + s2 in self.mp:
return self.mp[s1 + " " + s2]
n = len(s1)
for i in range(1, n):
if self.isScramble(s1[:i], s2[:i]) and self.isScramble(s1[i:], s2[i:]):
self.mp[s1 + " " + s2] = True
return True
elif self.isScramble(s1[:i], s2[n - i :]) and self.isScramble(
s1[i:], s2[: n - i]
):
self.mp[s1 + " " + s2] = True
return True
self.mp[s1 + " " + s2] = False
return False | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR STRING VAR VAR RETURN VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR NUMBER RETURN NUMBER |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def __init__(self):
self.map = {}
def isScramble(self, s1, s2):
if (s1, s2) in self.map:
return self.map[s1, s2]
if len(s1) != len(s2):
self.map[s1, s2] = False
return False
if s1 == s2:
self.map[s1, s2] = True
return True
if sorted(s1) != sorted(s2):
self.map[s1, s2] = False
return False
for i in range(1, len(s1)):
if self.isScramble(s1[:i], s2[:i]) and self.isScramble(s1[i:], s2[i:]):
return True
if self.isScramble(s1[:i], s2[len(s2) - i :]) and self.isScramble(
s1[i:], s2[: len(s2) - i]
):
return True
self.map[s1, s2] = False
return False | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, S1: str, S2: str):
s1, s2 = S1, S2
if len(s1) != len(s2) or sorted(s1) != sorted(s2):
return False
if s1 == s2:
return True
n = len(s1)
dp = [[([-1] * n) for _ in range(n)] for _ in range(n + 1)]
for i in range(n):
for j in range(n):
dp[1][i][j] = s1[i] == s2[j]
for k in range(2, n + 1):
for i in range(n - k + 1):
for j in range(n - k + 1):
dp[k][i][j] = False
for p in range(1, k):
if (
dp[p][i][j]
and dp[k - p][i + p][j + p]
or dp[p][i][j + k - p]
and dp[k - p][i + p][j]
):
dp[k][i][j] = True
break
return dp[n][0][0] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR NUMBER NUMBER |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | import sys
sys.setrecursionlimit(100000)
class Solution:
def __init__(self):
self.dp = dict()
def isScramble(self, S1: str, S2: str):
if len(S1) != len(S2):
return False
if S1 == S2:
return True
if S1 + S2 in self.dp:
return self.dp[S1 + S2]
count_string = len(S1)
for i in range(1, count_string):
if self.isScramble(S1[0:i], S2[0:i]) and self.isScramble(S1[i:], S2[i:]):
self.dp[S1 + S2] = True
return self.dp[S1 + S2]
if self.isScramble(S1[0:i], S2[-i:]) and self.isScramble(
S1[i:], S2[0 : count_string - i]
):
self.dp[S1 + S2] = True
return self.dp[S1 + S2]
self.dp[S1 + S2] = False
return self.dp[S1 + S2] | IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR VAR |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, S1: str, S2: str):
def helper(s1, s2, memo):
if (s1, s2) in memo:
return memo[s1, s2]
if sorted(s1) != sorted(s2):
memo[s1, s2] = False
return False
if s1 == s2:
memo[s1, s2] = True
return True
for i in range(1, len(s1)):
if (
helper(s1[:i], s2[:i], memo)
and helper(s1[i:], s2[i:], memo)
or helper(s1[:i], s2[-i:], memo)
and helper(s1[i:], s2[:-i], memo)
):
memo[s1, s2] = True
return True
memo[s1, s2] = False
return False
return helper(S1, S2, {}) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR DICT |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, s1: str, s2: str):
hsh = {}
return self.check(s1, s2, hsh)
def check(self, s1, s2, hsh):
n = len(s1)
if s1 == s2:
hsh[s1 + s1] = True
return True
if s1 + s2 in hsh:
return hsh[s1 + s2]
for i in range(1, n):
a = s1[:i]
b = s2[:i]
c = s1[i:]
d = s2[i:]
if self.check(a, b, hsh) and self.check(c, d, hsh):
hsh[s1 + s2] = True
return True
e = s1[:i]
f = s2[n - i :]
g = s1[i:]
h = s2[0 : n - i]
if self.check(e, f, hsh) and self.check(g, h, hsh):
hsh[s1 + s2] = True
return True
hsh[s1 + s2] = False
return False | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN NUMBER |
Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Scrambled string: Given string str, we can represent it as a binary tree by partitioning it into two non-empty substrings recursively.
Below is one possible representation of str = coder:
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node co and swap its two children, it produces a scrambled string ocder.
Similarly, if we continue to swap the children of nodes der and er, it produces a scrambled string ocred.
Note: Scrambled string is not the same as an Anagram.
Print "Yes" if S2 is a scrambled form of S1 otherwise print "No".
Example 1:
Input: S1="coder", S2="ocder"
Output: Yes
Explanation: ocder is a scrambled
form of coder.
ocder
/ \
oc der
/ \
o c
As "ocder" can represent it
as a binary tree by partitioning
it into two non-empty substrings.
We just have to swap 'o' and 'c'
to get "coder".
Example 2:
Input: S1="abcde", S2="caebd"
Output: No
Explanation: caebd is not a
scrambled form of abcde.
Your Task:
You don't need to read input or print anything. You only need to complete the function isScramble() which takes two strings S1 and S2 as input and returns a boolean value.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constrains:
S1.length = S2.length
S1.length<=31
S1 and S2 consist of lower-case English letters. | class Solution:
def isScramble(self, S1: str, S2: str):
if len(S1) != len(S2):
return False
self.mappa = dict()
self.s1 = S1
self.s2 = S2
self.n = len(S1)
return self.func(0, 0, self.n)
def func(self, ind1, ind2, size):
fetch = self.mappa.get((ind1, ind2, size))
if fetch is not None:
return fetch
else:
ans = False
if size == 1:
ans = self.s1[ind1] == self.s2[ind2]
for i in range(1, size):
ans |= self.func(ind1, ind2, i) and self.func(
ind1 + i, ind2 + i, size - i
)
ans |= self.func(ind1, ind2 + size - i, i) and self.func(
ind1 + i, ind2, size - i
)
self.mappa[ind1, ind2, size] = ans
return ans | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | n = int(input())
l = list(map(int, input().split()))
if n >= 130:
print("Yes")
else:
d = {}
for i in range(n):
if l[i] in d:
d[l[i]].append(i)
else:
d[l[i]] = [i]
f = False
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
v = l[i] ^ l[j] ^ l[k]
if v in d:
if len(d[v]) > 3:
f = True
else:
for x in d[v]:
if x != i and x != j and x != k:
f = True
if f:
print("Yes")
else:
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | def xorexists(n, A):
if n >= 130:
return True
else:
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
temp = A[i] ^ A[j] ^ A[k]
if temp in A[k + 1 :]:
return True
return False
n = int(input())
A = [int(j) for j in input().split()]
if xorexists(n, A):
print("Yes")
else:
print("No") | FUNC_DEF IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | n = int(input())
L = list(map(int, input().split()))
d = {}
a = 0
b = 0
for i in L:
try:
d[i] += 1
if d[i] == 2:
a += 1
elif d[i] == 4:
a -= 1
b += 1
break
except:
d[i] = 1
if a >= 2 or b >= 1:
print("Yes")
else:
d = {}
for i in range(1, n - 1):
x = L[i] ^ L[i + 1]
try:
d[x] += 1
except:
d[x] = 1
for i in range(1, n - 1):
x = L[i] ^ L[i + 1]
d[x] -= 1
x = L[i - 1] ^ L[i]
try:
if d[x] >= 1:
print("Yes")
break
except:
pass
if i == n - 2:
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | def nest(n):
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
for a in range(k + 1, n):
if l[i] ^ l[j] ^ l[k] ^ l[a] == 0:
return True
return False
n = int(input())
l = list(map(int, input().split()))
if n > 130:
print("Yes")
else:
f = 0
if nest(n):
print("Yes")
else:
print("No") | FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | from sys import stdin
def solve():
n = int(input())
d = {}
a = list(map(int, input().split()))
if n < 4:
return "No"
elif n >= 130:
return "Yes"
else:
for i in range(0, n):
for j in range(i + 1, n):
for k in range(j + 1, n):
for l in range(k + 1, n):
if a[i] ^ a[j] ^ a[k] ^ a[l] == 0:
return "Yes"
return "No"
print(solve()) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN STRING RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | n = int(input())
arr = list(map(int, input().split()))
def xor():
if n > 67:
print("Yes")
else:
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
for l in range(k + 1, n):
if arr[i] ^ arr[j] ^ arr[k] ^ arr[l] == 0:
print("Yes")
return
else:
pass
print("No")
xor() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | def fun(l, n):
if n >= 138:
return "Yes"
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
for l1 in range(k + 1, n):
c = l[i] ^ l[j] ^ l[k] ^ l[l1]
if c == 0:
return "Yes"
return "No"
n = int(input())
l = list(map(int, input().split()))
print(fun(l, n)) | FUNC_DEF IF VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | try:
n = int(input())
l = list(map(int, input().split()))
cont = 1
if n >= 130:
print("Yes")
cont = 0
if cont:
flag = 0
for x in range(n):
for y in range(x + 1, n):
for z in range(y + 1, n):
for a in range(z + 1, n):
if l[x] ^ l[y] ^ l[z] ^ l[a] == 0:
flag = 1
break
if flag:
break
if flag:
break
if flag:
break
if flag:
print("Yes")
else:
print("No")
except:
pass | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR IF VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | n = int(input())
a = list(map(int, input().split()))
if n >= 130:
print("Yes")
else:
ans = 0
for i in range(n - 3):
for j in range(i + 1, n - 2):
for k in range(j + 1, n - 1):
for l in range(k + 1, n):
if a[i] ^ a[j] ^ a[k] ^ a[l] == 0:
ans += 1
break
if ans == 1:
break
if ans == 1:
break
if ans == 1:
break
if ans == 1:
print("Yes")
else:
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | n = int(input())
arr = list(map(int, input().split()))
if n >= 130:
print("Yes")
else:
_map = {}
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
a = arr[i] ^ arr[j]
if a in _map:
if _map[a][1] < i:
print("Yes")
exit()
else:
_map[a] = i, j
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | try:
def gray(n, arr):
if n < 4:
return False
elif n > 130:
return True
else:
for i in range(0, n):
for j in range(i + 1, n):
for k in range(j + 1, n):
for w in range(k + 1, n):
if arr[i] ^ arr[j] ^ arr[k] ^ arr[w] == 0:
return True
return False
n = int(input())
arr = list(map(int, input().split()))
if gray(n, arr):
print("Yes")
else:
print("No")
except:
pass | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | def greysim(n, A):
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
for l in range(k + 1, n):
if A[i] ^ A[j] ^ A[k] ^ A[l] == 0:
print("Yes")
return
print("No")
return
n = int(input())
list1 = list(map(int, input().split()))
if n >= 138:
print("Yes")
else:
greysim(n, list1) | FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | n = int(input())
A = [int(i) for i in input().strip().split()]
s = set()
if n >= 130:
print("Yes")
else:
check = False
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if A[i] ^ A[j] ^ A[k] in s:
check = True
break
if check:
break
if check:
break
s.add(A[i])
if check:
print("Yes")
else:
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | a = int(input())
table = {}
bb = list(map(int, input().split()))
for i in range(len(bb)):
table[bb[i]] = i
t = 0
if a >= 130:
print("Yes")
else:
for i in range(a):
for j in range(i + 1, a):
for k in range(j + 1, a):
c = bb[i] ^ bb[j] ^ bb[k]
if c in table and table[c] > k:
print("Yes")
t = 1
break
if t == 1:
break
if t == 1:
break
else:
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | def compute(a):
n = len(a)
res = "No"
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
for l in range(k + 1, n):
if a[i] ^ a[j] ^ a[k] ^ a[l] == 0:
res = "Yes"
return res
return res
n = int(input())
a = [int(i) for i in input().split()]
if n >= 130:
print("Yes")
else:
print(compute(a)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | n = int(input())
a = list(map(int, input().split()))
if n >= 130:
print("Yes")
else:
flag = 0
i = 0
while i < n:
j = i + 1
while j < n:
k = j + 1
while k < n:
l = k + 1
while l < n:
if a[i] ^ a[j] ^ a[k] ^ a[l] == 0:
flag = 1
break
l += 1
if flag == 1:
break
k += 1
if flag == 1:
break
j += 1
if flag == 1:
break
i += 1
if flag == 1:
print("Yes")
else:
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | n = int(input())
arr = list(map(int, input().split()))
limit = 64 * 2 + 10
if n >= limit:
print("Yes")
else:
flag = 0
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
for k in range(j + 1, len(arr)):
for l in range(k + 1, len(arr)):
if arr[i] ^ arr[j] ^ arr[k] ^ arr[l] == 0:
print("Yes")
flag = 1
break
if flag == 1:
break
if flag == 1:
break
if flag == 1:
break
if flag == 0:
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | def binary_search(a, x):
left = 0
right = len(a) - 1
while left <= right:
mid = (left + right) // 2
if a[mid] == x:
return True
elif a[mid] > x:
right = mid - 1
else:
left = mid + 1
return False
def search(a, x, i, j, k):
n = len(a)
for ni in range(n):
if ni == i or ni == j or ni == k:
continue
elif a[ni] == x:
return True
return False
def compute(a):
n = len(a)
res = "No"
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
temp = a[i] ^ a[j] ^ a[k]
if search(a, temp, i, j, k) == True:
res = "Yes"
return res
return res
n = int(input())
a = [int(i) for i in input().split()]
if n >= 130:
print("Yes")
else:
a.sort()
print(compute(a)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | T = int(input())
if T >= 130:
print("Yes")
exit()
num = []
num = list(map(int, input().split()))
for i in range(T):
for j in range(i + 1, T):
for k in range(j + 1, T):
for l in range(k + 1, T):
if num[i] ^ num[j] ^ num[k] ^ num[l] == 0:
print("Yes")
exit()
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | def check(arr):
for i in range(0, len(arr) - 3):
for j in range(i + 1, len(arr) - 2):
for k in range(j + 1, len(arr) - 1):
for x in range(k + 1, len(arr)):
if arr[i] ^ arr[j] ^ arr[k] ^ arr[x] == 0:
return "Yes"
return "No"
try:
n = int(input())
arr = list(map(int, input().strip().split(" ")))
if n > 130:
print("Yes")
else:
print(check(arr))
except:
pass | FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
The Gray code (see wikipedia for more details) is a well-known concept.
One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequence A[1..n] (0≤A[i]<2^{64}), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 ≤ i1 < i2 < i3 < i4 ≤ n) out of the given n numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Here xor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
------ Input ------
First line contains one integer n (4≤n≤100000).
Second line contains n space seperated non-negative integers denoting the sequence A.
------ Output ------
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
----- Sample Input 1 ------
5
1 0 2 3 7
----- Sample Output 1 ------
Yes | n = int(input())
m = [*map(int, input().split())]
if n > 130:
print("Yes")
exit(0)
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
t = m[i] ^ m[j] ^ m[k]
if t in m[k + 1 :]:
print("Yes")
exit(0)
print("No") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
ans = []
arr = []
digmap = dict()
digmap[2] = ["a", "b", "c"]
digmap[3] = ["d", "e", "f"]
digmap[4] = ["g", "h", "i"]
digmap[5] = ["j", "k", "l"]
digmap[6] = ["m", "n", "o"]
digmap[7] = ["p", "q", "r", "s"]
digmap[8] = ["t", "u", "v"]
digmap[9] = ["w", "x", "y", "z"]
def funcall(index, arr):
if index == N:
ans.append("".join(arr))
return
for char in digmap[a[index]]:
arr.append(char)
funcall(index + 1, arr)
arr.pop()
funcall(0, arr)
if ans == [""]:
return []
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER LIST STRING STRING STRING ASSIGN VAR NUMBER LIST STRING STRING STRING ASSIGN VAR NUMBER LIST STRING STRING STRING ASSIGN VAR NUMBER LIST STRING STRING STRING ASSIGN VAR NUMBER LIST STRING STRING STRING ASSIGN VAR NUMBER LIST STRING STRING STRING STRING ASSIGN VAR NUMBER LIST STRING STRING STRING ASSIGN VAR NUMBER LIST STRING STRING STRING STRING FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR IF VAR LIST STRING RETURN LIST RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def check(self, a, i, out, n, dic, ans):
if i >= n:
ans.append("".join(out))
return
for j in range(len(dic[a[i]])):
out.append(dic[a[i]][j])
self.check(a, i + 1, out, n, dic, ans)
out.pop()
def possibleWords(self, a, N):
dic = {
(0): "",
(1): "",
(2): ["a", "b", "c"],
(3): ["d", "e", "f"],
(4): ["g", "h", "i"],
(5): ["j", "k", "l"],
(6): ["m", "n", "o"],
(7): ["p", "q", "r", "s"],
(8): ["t", "u", "v"],
(9): ["w", "x", "y", "z"],
}
ans = []
self.check(a, 0, [], N, dic, ans)
return ans | CLASS_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER LIST VAR VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
ans = ""
res = []
dirc = {
(1): [],
(2): ["a", "b", "c"],
(3): ["d", "e", "f"],
(4): ["g", "h", "i"],
(5): ["j", "k", "l"],
(6): ["m", "n", "o"],
(7): ["p", "q", "r", "s"],
(8): ["t", "u", "v"],
(9): ["w", "x", "y", "z"],
}
def getAns(ind, ans, res, dirc):
if ind == N:
res.append(ans)
return
num = a[ind]
lett = dirc[num]
for i in lett:
ans += i
getAns(ind + 1, ans, res, dirc)
ans = ans[:-1]
getAns(0, ans, res, dirc)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
res = {
(2): ["a", "b", "c"],
(3): ["d", "e", "f"],
(4): ["g", "h", "i"],
(5): ["j", "k", "l"],
(6): ["m", "n", "o"],
(7): ["p", "q", "r", "s"],
(8): ["t", "u", "v"],
(9): ["w", "x", "y", "z"],
}
def recurs(i, vals):
nonlocal result
if len(vals) == N:
result.append("".join(vals.copy()))
return
for x in res[a[i]]:
recurs(i + 1, vals + [x])
result = []
recurs(0, [])
return sorted(result) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR RETURN FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER LIST RETURN FUNC_CALL VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
keys = {
(2): ("a", "b", "c"),
(3): ("d", "e", "f"),
(4): ("g", "h", "i"),
(5): ("j", "k", "l"),
(6): ("m", "n", "o"),
(7): ("p", "q", "r", "s"),
(8): ("t", "u", "v"),
(9): ("w", "x", "y", "z"),
}
res = []
def helper(idx, temp):
if len(temp) == N:
res.append(temp[:])
return
for i in range(idx, len(a)):
for val in keys[a[i]]:
temp += val
helper(i + 1, temp)
temp = temp[:-1]
helper(0, "")
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def util(self, a, d, i, n, ans):
global fans
if i == n:
fans.append(ans)
return
for j in d[a[i]]:
self.util(a, d, i + 1, n, ans + j)
def possibleWords(self, a, N):
global fans
fans = []
d = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
self.util(a, d, 0, N, "")
return fans | CLASS_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR STRING RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def helper(self, index, a, N, curr, d, res):
if index == N:
res.append(curr)
return
val = a[index]
word = d[val]
for letter in word:
curr += letter
self.helper(index + 1, a, N, curr, d, res)
curr = curr[:-1]
return res
def possibleWords(self, a, N):
d = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
res = []
self.helper(0, a, N, "", d, res)
return res | CLASS_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR STRING VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | from itertools import product
class Solution:
def possibleWords(self, a, N):
dict = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
if not a:
return []
lst = []
for i in a:
i = str(i)
if i in dict:
lst.append(dict[i])
ans = product(*lst)
res = []
for i in ans:
res.append("".join(i))
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING IF VAR RETURN LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | def solve(a, output, index, ans, mapping):
if index >= len(a):
ans.append(output)
return
number = a[index]
value = mapping[number]
for i in range(len(value)):
output += value[i]
solve(a, output, index + 1, ans, mapping)
output = output[: len(output) - 1]
class Solution:
def possibleWords(self, a, N):
ans = []
output = ""
index = 0
mapping = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
solve(a, output, index, ans, mapping)
return ans | FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, n):
b = []
ans = []
ind_max = []
ind = [(0) for i in range(n)]
fac = [(1) for i in range(n)]
for i in range(n):
if a[i] == 2:
s = "abc"
elif a[i] == 3:
s = "def"
elif a[i] == 4:
s = "ghi"
elif a[i] == 5:
s = "jkl"
elif a[i] == 6:
s = "mno"
elif a[i] == 7:
s = "pqrs"
elif a[i] == 8:
s = "tuv"
elif a[i] == 9:
s = "wxyz"
b.append(s)
ind_max.append(len(s))
f = 1
for i in range(n - 1, 0, -1):
if a[i] == 7 or a[i] == 9:
fac[i - 1] = fac[i] * 4
else:
fac[i - 1] = fac[i] * 3
if a[0] == 7 or a[0] == 9:
t = fac[0] * 4
else:
t = fac[0] * 3
i = 1
while i <= t:
p = ""
for j in range(n):
p += b[j][ind[j]]
ans.append(p)
x = n - 1
while x >= 0:
if i % fac[x] == 0:
if ind[x] == ind_max[x] - 1:
ind[x] = 0
else:
ind[x] += 1
else:
break
x -= 1
i += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
wordmap = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
def possibleWords(self, a, N):
res = []
self.possibleWordsHelper(a, N, 0, "", res)
res.sort()
return res
def possibleWordsHelper(self, a, N, i, s, res):
if i == N:
res.append(s)
return
chars = list(self.wordmap.get(a[i]))
for c in chars:
self.possibleWordsHelper(a, N, i + 1, s + c, res) | CLASS_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER STRING VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, arr, N):
hm = {
(1): "",
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
(10): "*",
(11): "0",
(12): "#",
}
ans = []
def solve(i, curr):
if i == N:
ans.append("".join(curr.copy()))
return
key = arr[i]
for ch in hm[key]:
curr.append(ch)
solve(i + 1, curr)
curr.pop()
solve(0, [])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR RETURN ASSIGN VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
digit = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
def f(ind, path):
if ind == N:
res.append(path)
return
for j in digit[a[ind]]:
path += j
f(ind + 1, path)
path = path[:-1]
res = []
f(0, "")
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
codes = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
if len(a) == 0:
res = []
res.append("")
return res
ch = a[0]
ros = a[1:]
rres = self.possibleWords(ros, N)
code_val = codes[ch]
mres = []
for i in code_val:
for r in rres:
mres.append(i + r)
return mres | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, arr, N):
d = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
o = []
def recurr(d, i, out, arr, o):
if i == len(arr):
o.append("".join(out))
return
for j in range(len(d[arr[i]])):
if arr[i] == 1 or arr[i] == 0:
return
recurr(d, i + 1, out + [d[arr[i]][j]], arr, o)
recurr(d, 0, [], arr, o)
return o | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER LIST VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
d = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
ans = [""]
for r in range(N):
ans = [(i + j) for j in d[a[r]] for i in ans]
return sorted(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
arr = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
output = ""
ans = []
ind = 0
def find(ind, cur, output, ans, a, arr):
if cur == N:
ans.append(output)
return
for i in range(len(arr[a[cur]])):
val = arr[a[cur]][i]
output = output + val
find(ind, cur + 1, output, ans, a, arr)
output = output[0:-1]
if a[cur] == 0 or a[cur] == 1:
return
find(0, 0, output, ans, a, arr)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def __init__(self):
self.mapping = {
(2): ["a", "b", "c"],
(3): ["d", "e", "f"],
(4): ["g", "h", "i"],
(5): ["j", "k", "l"],
(6): ["m", "n", "o"],
(7): ["p", "q", "r", "s"],
(8): ["t", "u", "v"],
(9): ["w", "x", "y", "z"],
}
self.strings = []
def recursive_string_builder(self, str1, keys):
if keys == []:
self.strings.append(str1)
return
else:
x = keys.pop(0)
for i in self.mapping.get(x):
self.recursive_string_builder(str1 + i, keys.copy())
def possibleWords(self, a, N):
self.recursive_string_builder("", a)
return self.strings | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING ASSIGN VAR LIST FUNC_DEF IF VAR LIST EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR STRING VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, n):
def solve(index, temp, ans, s, n):
if index == n:
ans.append(s)
return
for j in temp[index]:
s += j
solve(index + 1, temp, ans, s, n)
s = s[:-1]
d = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
ans = []
temp = []
for i in a:
temp.append(d[i])
s = ""
solve(0, temp, ans, s, n)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def Phone_number(self, arr, n, i, str1, res, op):
if i == n:
res.append(op)
return
no = str1[arr[i]]
for op1 in no:
op1 = op + op1
self.Phone_number(arr, n, i + 1, str1, res, op1)
op1 = "".join([op1[i] for i in range(len(op1) - 1)])
def possibleWords(self, a, N):
res = []
str1 = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
self.Phone_number(a, N, 0, str1, res, "")
return res | CLASS_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR STRING RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
m = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
phnum = ""
for i in a:
phnum += str(i)
def combinations(phnum):
if len(phnum) == 1:
return list(m[phnum])
result1 = combinations(phnum[:-1])
result2 = m[phnum[-1]]
return [(ch1 + ch2) for ch1 in result1 for ch2 in result2]
return combinations(phnum) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
all = {
(2): ["a", "b", "c"],
(3): ["d", "e", "f"],
(4): ["g", "h", "i"],
(5): ["j", "k", "l"],
(6): ["m", "n", "o"],
(7): ["p", "q", "r", "s"],
(8): ["t", "u", "v"],
(9): ["w", "x", "y", "z"],
}
ret = []
for d in a:
if len(ret) == 0:
ret = all[d]
else:
copy = []
for r in ret:
for a in all[d]:
copy.append(r + a)
ret = copy
return ret | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def hell(self, a, i, N, cur, dic, res):
if len(cur) == len(a):
res.append(cur)
return
x = dic.get(a[i])
for j in x:
cur = cur + j
Solution.hell(self, a, i + 1, N, cur, dic, res)
cur = cur[: len(cur) - 1]
return res
def possibleWords(self, a, N):
cur = ""
res = []
dic = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
return Solution.hell(self, a, 0, N, cur, dic, res) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
letters = [
["a", "b", "c"],
["d", "e", "f"],
["g", "h", "i"],
["j", "k", "l"],
["m", "n", "o"],
["p", "q", "r", "s"],
["t", "u", "v"],
["w", "x", "y", "z"],
]
i = 0
ans = []
str = ""
self.permutation(str, a, i, N, ans, letters)
return ans
def permutation(self, str, a, i, N, ans, letters):
if i == N:
ans.append(str)
else:
for j in letters[a[i] - 2]:
self.permutation(str + j, a, i + 1, N, ans, letters) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
ans = []
dic = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
def helper(i, a, N):
if i == N:
return [""]
ans = helper(i + 1, a, N)
output = []
for child in dic[a[i]]:
for ele in ans:
output.append(child + ele)
return output
return helper(0, a, N) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF IF VAR VAR RETURN LIST STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | keypad = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
class Solution:
def possibleWords(self, a, N):
def helper(i, w, res, a, N):
if i == N:
res.append(w)
return
for j in range(len(keypad[a[i]])):
helper(i + 1, w + keypad[a[i]][j], res, a, N)
res = []
helper(0, "", res, a, N)
return res | ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER STRING VAR VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
keypad = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
def generate_list(list_to_be_generated, keypad, number):
if len(list_to_be_generated) == 0:
string = keypad[number]
for i in string:
list_to_be_generated.append(i)
else:
string = keypad[number]
newlist = []
for i in string:
for j in range(len(list_to_be_generated)):
newlist.append(list_to_be_generated[j] + i)
list_to_be_generated = newlist
return list_to_be_generated
generated_list = []
for number in range(N):
generated_list = generate_list(generated_list, keypad, a[number])
return sorted(generated_list) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
keymap = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
output = []
def recursive(curr=0, word=""):
if curr == N:
return output.append(word)
for i in range(len(keymap[a[curr]])):
word = word + keymap[a[curr]][i]
recursive(curr + 1, word)
word = word[:-1]
if a[curr] == 1:
recursive(curr + 1, word)
recursive()
return output | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FUNC_DEF NUMBER STRING IF VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
res = []
self.func(a, N, 0, res, "")
return res
def func(self, a, N, index, res, s):
if index == N:
if len(s) > 0:
res.append(s)
return res
temp = a[index]
if temp == 0 or temp == 1:
self.func(a, N, index + 1, res, s)
return res
mob = [
[],
[],
["a", "b", "c"],
["d", "e", "f"],
["g", "h", "i"],
["j", "k", "l"],
["m", "n", "o"],
["p", "q", "r", "s"],
["t", "u", "v"],
["w", "x", "y", "z"],
]
temp_str = s
for i in range(len(mob[temp])):
s += mob[temp][i]
self.func(a, N, index + 1, res, s)
s = temp_str
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER VAR STRING RETURN VAR FUNC_DEF IF VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR LIST LIST LIST LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
res = []
digg = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
def back(i, curr):
if len(curr) == len(a):
res.append(curr)
return
for char in digg[a[i]]:
back(i + 1, curr + char)
if a:
back(0, "")
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
c = ""
for i in a:
c += str(i)
def helper(i, m):
if i > N:
return
if i == N:
new = ""
for j in subset:
new += j
ans.append(new)
return
str = m[c[i]]
for j in range(len(str)):
subset.append(str[j])
helper(i + 1, m)
subset.pop()
m = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
if not c:
return []
ans = []
subset = []
helper(0, m)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING IF VAR RETURN LIST ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
ans = []
def create(idx, temp):
if idx == N:
ans.append(temp)
return
for c in m[a[idx]]:
create(idx + 1, temp + c)
m = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
create(0, "")
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
hash = [".", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
def comb(index, n, str, out, ll):
if index == n:
ll.append(out)
return ll
tt = hash[str[index] - 1]
for i in range(len(tt)):
k = out
val = hash[str[index] - 1]
out += val[i]
ll = comb(index + 1, n, str, out, ll)
out = k
return ll
lis = comb(0, N, a, "", [])
lis.sort()
return lis | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR STRING LIST EXPR FUNC_CALL VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | import itertools as it
class Solution:
def possibleWords(self, a, N):
m = {
(2): ["a", "b", "c"],
(3): ["d", "e", "f"],
(4): ["g", "h", "i"],
(5): ["j", "k", "l"],
(6): ["m", "n", "o"],
(7): ["p", "q", "r", "s"],
(8): ["t", "u", "v"],
(9): ["w", "x", "y", "z"],
}
k = [m[i] for i in a]
l = []
for r in list(it.product(*k)):
l.append("".join(r))
return l | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
arr = [
[],
[],
["a", "b", "c"],
["d", "e", "f"],
["g", "h", "i"],
["j", "k", "l"],
["m", "n", "o"],
["p", "q", "r", "s"],
["t", "u", "v"],
["w", "x", "y", "z"],
]
sub = ""
res = []
self.func(a, N, arr, sub, 0, res)
return res
def func(self, a, n, arr, sub, k, res):
if k == n:
res.append(sub)
else:
self.func(a, n, arr, sub + arr[a[k]][0], k + 1, res)
self.func(a, n, arr, sub + arr[a[k]][1], k + 1, res)
self.func(a, n, arr, sub + arr[a[k]][2], k + 1, res)
if a[k] == 7 or a[k] == 9:
self.func(a, n, arr, sub + arr[a[k]][3], k + 1, res) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST LIST LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING ASSIGN VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | def solve(a, output, ans, d, index, N):
if index == N:
s = "".join(output[:])
ans.append(s)
return ans
num = a[index]
value = d[num]
for i in range(len(value)):
output.append(value[i])
solve(a, output, ans, d, index + 1, N)
output.pop(-1)
class Solution:
def possibleWords(self, a, N):
ans = []
d = dict()
d = {
(0): "",
(1): "",
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
output = []
solve(a, output, ans, d, 0, N)
return ans | FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def __init__(self):
self.phone_dict = dict()
self.phone_dict[2] = "abc"
self.phone_dict[3] = "def"
self.phone_dict[4] = "ghi"
self.phone_dict[5] = "jkl"
self.phone_dict[6] = "mno"
self.phone_dict[7] = "pqrs"
self.phone_dict[8] = "tuv"
self.phone_dict[9] = "wxyz"
self.arr1 = []
def pW(self, a, N, i=0, created=""):
if i == N:
self.arr1.append(created)
return
for chr in self.phone_dict[a[i]]:
self.pW(a, N, i + 1, created + chr)
def possibleWords(self, a, N):
self.pW(a, N)
return self.arr1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR LIST FUNC_DEF NUMBER STRING IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
def helper(cur, ind, a, N, res, mapp):
if ind == N:
res.append(cur)
return res
for i in mapp[a[ind]]:
helper(cur + i, ind + 1, a, N, res, mapp)
mapp = {
(2): ["a", "b", "c"],
(3): ["d", "e", "f"],
(4): ["g", "h", "i"],
(5): ["j", "k", "l"],
(6): ["m", "n", "o"],
(7): ["p", "q", "r", "s"],
(8): ["t", "u", "v"],
(9): ["w", "x", "y", "z"],
}
res = []
helper("", 0, a, N, res, mapp)
res.sort()
return res | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR STRING NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def helper(self, N, curr, i, l):
if N == i:
l.append(curr)
return
if len(keys[i]) == 3:
self.helper(N, curr + keys[i][0], i + 1, l)
self.helper(N, curr + keys[i][1], i + 1, l)
self.helper(N, curr + keys[i][2], i + 1, l)
else:
self.helper(N, curr + keys[i][0], i + 1, l)
self.helper(N, curr + keys[i][1], i + 1, l)
self.helper(N, curr + keys[i][2], i + 1, l)
self.helper(N, curr + keys[i][3], i + 1, l)
def get_keys(self, a):
d = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
keys = []
for k in a:
keys.append(d[k])
return keys
def possibleWords(self, a, N):
global keys
keys = self.get_keys(a)
l = []
self.helper(N=N, curr="", i=0, l=l)
l.sort()
return l | CLASS_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR STRING NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | numbs = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
class Solution:
def possibleWords(self, a, N):
num = ""
for i in a:
num += str(i)
ans = self.combo(num)
return ans
def combo(self, phnum):
if len(phnum) == 1:
return list(numbs[phnum[0]])
else:
result = self.combo(phnum[:-1])
return [(ch1 + ch2) for ch1 in result for ch2 in numbs[phnum[-1]]] | ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING CLASS_DEF FUNC_DEF ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
phone_dict = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
arr = [phone_dict[i] for i in a]
def phone_digit(n, arr, output, container):
if n == 0:
container.append(output)
return
for i in range(len(arr[0])):
phone_digit(n - 1, arr[1:], output + arr[0][i], container)
arr.pop(0)
return
container = []
phone_digit(N, arr, "", container)
return container | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR STRING VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
keypad = {
(1): [],
(2): ["a", "b", "c"],
(3): ["d", "e", "f"],
(4): ["g", "h", "i"],
(5): ["j", "k", "l"],
(6): ["m", "n", "o"],
(7): ["p", "q", "r", "s"],
(8): ["t", "u", "v"],
(9): ["w", "x", "y", "z"],
}
def possibleWordsRec(a, N, words):
if N == 0:
return words
else:
chars = keypad[a[0]]
words_tmp = []
if len(words) == 0:
words_tmp = chars
else:
for w in words:
for c in chars:
words_tmp.append(w + c)
del a[0]
words = words_tmp
return possibleWordsRec(a, N - 1, words)
result = possibleWordsRec(a, N, [])
return result | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, arr, N):
dictt = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
res = []
self.dfs(dictt, arr, "", res)
return res
def dfs(self, dictt, arr, path, res):
if len(arr) == 0:
res.append(path)
return
for e in dictt[arr[0]]:
self.dfs(dictt, arr[1:], path + e, res) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR STRING VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
characters = "abcdefghijklmnopqrstuvwxyz"
st = 0
numToLetters = {}
for i in range(2, 10):
if i == 7 or i == 9:
ed = st + 4
else:
ed = st + 3
numToLetters[i] = [st, ed]
st = ed
self.pool = a
self.possibilities = []
self.word = ""
self.numToLetters = numToLetters
self.characters = characters
self.populate()
return self.possibilities
def populate(self, idx=0, word=""):
st, ed = self.numToLetters[self.pool[idx]]
for char in self.characters[st:ed]:
myword = word + char
if idx == len(self.pool) - 1:
self.possibilities.append(myword)
else:
self.populate(idx + 1, myword)
return | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF NUMBER STRING ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def helper(self, input_arr, n, temp, idx, result, button_arr):
if idx == n:
result.append(temp)
return
j = 0
while j < len(button_arr[input_arr[idx]]):
self.helper(
input_arr,
n,
temp + button_arr[input_arr[idx]][j],
idx + 1,
result,
button_arr,
)
j += 1
def possibleWords(self, a, N):
result = []
button_arr = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
self.helper(a, N, "", 0, result, button_arr)
return result | CLASS_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR VAR STRING NUMBER VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
d = {
(1): "",
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
ans = []
w = ""
for i in a:
w = w + str(i)
def poss(out, ind):
if ind == len(w):
ans.append(out)
return
curr_ind = int(w[ind])
word = d[curr_ind]
for i in word:
poss(out + i, ind + 1)
poss("", 0)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING NUMBER RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def __init__(self):
self.list_numbers = [
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz",
]
self.char_list = []
def prepare_words_phone_digits(self, l, ch_str):
if len(l) == 1:
for ch in l[0]:
self.char_list.append(ch_str + ch)
elif len(l) >= 2:
for ch in l[0]:
self.prepare_words_phone_digits(l[1:], ch_str + ch)
def possibleWords(self, a, N):
l = []
for i in a:
l.append(self.list_numbers[i - 1])
self.prepare_words_phone_digits(l, "")
return self.char_list | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
possible_nums = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
def possibleWords(self, a, N, i=0, permute=""):
if N > len(a):
N = len(a)
if i == N - 1:
return [(permute + x) for x in Solution.possible_nums[a[i]]]
curr = Solution.possible_nums[a[i]]
ans = []
for j in range(len(curr)):
ans += self.possibleWords(a, N, i + 1, permute + curr[j])
return ans | CLASS_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF NUMBER STRING IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
def numb(s1, s2):
x = []
for i in d[s1]:
for j in d[s2]:
y = i + j
x.append(y)
d[s1 + s2] = x
return s1 + s2
d = {
"2": ["a", "b", "c"],
"3": ["d", "e", "f"],
"4": ["g", "h", "i"],
"5": ["j", "k", "l"],
"6": ["m", "n", "o"],
"7": ["p", "q", "r", "s"],
"8": ["t", "u", "v"],
"9": ["w", "x", "y", "z"],
}
strr = ""
for e in a:
strr = strr + str(e)
if len(strr) == 2:
x = numb(strr[0], strr[1])
elif len(strr) == 1:
return d[strr]
else:
x = numb(strr[0], strr[1])
for i in range(2, len(strr)):
x = numb(x, strr[i])
return d[x] | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def possibleWords(self, a, N):
def calc(ind, s):
if ind == N:
ans.append(s)
return
for i in dic[a[ind]]:
s += i
calc(ind + 1, s)
s = s[:-1]
ans = []
dic = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
calc(0, "")
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
_dict = {
(1): (),
(2): ("a", "b", "c"),
(3): ("d", "e", "f"),
(4): ("g", "h", "i"),
(5): ("j", "k", "l"),
(6): ("m", "n", "o"),
(7): ("p", "q", "r", "s"),
(8): ("t", "u", "v"),
(9): ("w", "x", "y", "z"),
}
def possibleWords(self, a, N):
ls = []
self.helper(a, N, 0, "", ls)
return ls
def helper(self, a, N, idx, op_str, ls):
if idx == N:
ls.append(op_str)
return
chars = self._dict[a[idx]]
for ch in chars:
self.helper(a, N, idx + 1, op_str + ch, ls) | CLASS_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER STRING VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR |
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
Example 1:
Input: N = 3, a[] = {2, 3, 4}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Explanation: When we press 2,3,4 then
adg, adh, adi, ... cfi are the list of
possible words.
Example 2:
Input: N = 3, a[] = {3, 4, 5}
Output:
dgj dgk dgl dhj dhk dhl dij dik dil
egj egk egl ehj ehk ehl eij eik eil
fgj fgk fgl fhj fhk fhl fij fik fil
Explanation: When we press 3,4,5 then
dgj, dgk, dgl, ... fil are the list of
possible words.
Your Task:
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
Expected Time Complexity: O(4^{N}^{ }* N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ N ≤ 10
2 ≤ a[i] ≤ 9 | class Solution:
def __init__(self):
self.phone = {
(1): [],
(2): ["a", "b", "c"],
(3): ["d", "e", "f"],
(4): ["g", "h", "i"],
(5): ["j", "k", "l"],
(6): ["m", "n", "o"],
(7): ["p", "q", "r", "s"],
(8): ["t", "u", "v"],
(9): ["w", "x", "y", "z"],
(0): [],
}
def possibleWords(self, a, N):
self.n = N
return self.generate(0, a, "", [])
def generate(self, i, a, message, ans):
if i == self.n:
ans.append(message)
return ans
letters = self.phone[a[i]]
if letters:
for j in range(len(letters)):
ans = self.generate(i + 1, a, message + letters[j], ans)
else:
ans = self.generate(i + 1, a, message, ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST FUNC_DEF ASSIGN VAR VAR RETURN FUNC_CALL VAR NUMBER VAR STRING LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.