Problem:


Score : 400 points

Problem Statement

In this problem, we only consider strings consisting of lowercase English letters.

Strings s and t are said to be isomorphic when the following conditions are satisfied:

|s| = |t| holds.

For every pair i, j, one of the following holds:

s_i = s_j and t_i = t_j.

s_i \neq s_j and t_i \neq t_j.

For example, abcac and zyxzx are isomorphic, while abcac and ppppp are not.

A string s is said to be in normal form when the following condition is satisfied:

For every string t that is isomorphic to s, s \leq t holds. Here \leq denotes lexicographic comparison.

For example, abcac is in normal form, but zyxzx is not since it is isomorphic to abcac, which is lexicographically smaller than zyxzx.

You are given an integer N.
Print all strings of length N that are in normal form, in lexicographically ascending order.

Constraints

1 \leq N \leq 10

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Assume that there are K strings of length N that are in normal form: w_1, \ldots, w_K in lexicographical order.
Output should be in the following format:

w_1
:
w_K

Sample Input 1

1

Sample Output 1

a

Sample Input 2

2

Sample Output 2

aa
ab



Sample input:
1


Sample output:
a

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import string
import sys
sys.setrecursionlimit(10 ** 7)
from functools import lru_cache

N = int(input())
if N == 1:
    print('b')
    exit()

alph = string.ascii_lowercase

ans = defaultdict(int)

@lru_cache(maxsize=None)
def search(S, nums):
    if nums in ans:
        return
    if len(nums) == N:
        ans[nums] = S
        return

    for i in range(len(nums)):
        for s, n in zip(S, nums):
            if n == i:
                search(S + s, nums + (n, ))
                break
    C = set(list(S))
    for s in alph:
        if not s in C:
            search(S + s, nums + (len(nums), ))
            break

search('a', (0, ))
ans = list(ans.values())
ans.sort()
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
N = int(input())
if N==1:
    print('a')
    exit()

ans = []
for i in range(1<<(N-1)):
    s = 'a'
    for j in range(N-1):
        if i&(1<<j):
            s += s[-1]
        else:
            s += ('a' if s[-1]=='b' else 'b')
    ans.append(s)
print(*sorted(ans), sep='\n')

Submission status: Wrong Answer

Solution code:
N = int(input())

words = [chr(ord('a')+i) for i in range(10)]
dp = ['']
for i in range(1, N+1):
    ndp = []
    for w in dp:
        for k in range(i):
            ndp.append(w+words[k])
    dp = ndp
for l in dp:
    print(l)

Submission status: Wrong Answer

Solution code:
n = int(input())
alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]
def doukei(n):
  if n == 1:
    return ["a"]
  k = doukei(n-1)
  ans = []
  for i in range(len(k)):
    key = k[i][-1]
    ans.append(k[i]+"a")
    for j in range(1, 10):
      ans.append(k[i]+alpha[j])
      if key == alpha[j-1]:
        break
  return ans
answer = doukei(n)
for i in range(len(answer)):
  print(answer[i])

Submission status: Wrong Answer

Solution code:
from itertools import product
N=int(input())
s="abcdefghij"
for i in range(N):
  c=s[:i+1]
  for p in product(c,repeat=N-i-1):
    print(c+"".join(p))

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
x = list(product([0, 1], repeat=N))
ans = []
for i in range(1<<N-1):
	s = list(x[i])
	for j in range(N):
		if s[j] == 0:
			s[j] = "a"
		else:
			s[j] = "b"
	ans.append("".join(s))

print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
n = int(input())
anstmp = ["a"]
cnt = 0
for i in anstmp:
  t = ord(i[-1])-95
  if cnt > 100000:
    break
  for j in range(1,t+1):
    anstmp.append(i+chr(j+96))
  cnt += 1
for i in anstmp:
  if len(i) == n:
    print(i)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline

res = []
def fill(K, f):
    if K.count(-1) == 0:
        res.append(K)
        return 
    else:
        idx = []
        for i in range(N):
            if K[i] == -1:
                idx.append(i)
        kc = len(idx)
        for S in range(1, 1<<kc):
            if not S & 1:
                continue
            K2 = K[:]
            for j in range(kc):
                if S & (1<<j):
                    K2[idx[j]] = f
            fill(K2, f+1)
            
            
N = int(readline())
fill([-1]*N, 0)
res = [''.join(map(lambda x: chr(x+97), r)) for r in res]
print('\n'.join(res))

Submission status: Wrong Answer

Solution code:
n = int(input())
def f(l):
	return "".join([chr(x+97) for x in l])

a = [0 for _ in range(n)]
print(f(a))

while a != list(range(n)):
	for i in range(n-1, 0, -1):
		if a[i-1] >= a[i]:
			a[i] += 1
			for j in range(i+1, n):
				a[j] = 0
			print(f(a))
			break

Submission status: Wrong Answer

Solution code:
n = int(input())
alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "k", "l"]
def doukei(n):
  if n == 1:
    return ["a"]
  k = doukei(n-1)
  ans = []
  for i in range(len(k)):
    key = k[i][-1]
    ans.append(k[i]+"a")
    for j in range(1, 10):
      ans.append(k[i]+alpha[j])
      if key == alpha[j-1]:
        break
  return ans
answer = doukei(n)
for i in range(len(answer)):
  print(answer[i])

Submission status: Wrong Answer

Solution code:
N = int(input())

#from collections import deque

#Q = deque()

S = "a"

def printer(S):
    if len(S)==N:
        print(S)
    else:
        printer(S + S[-1])
        printer(S + chr(ord(S[-1])+1))

printer(S)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=[]
def b(s):
  if len(s)==n:
    ans.append(s)
    return
  for i in "abcdefghijklmnopqrstuvwxyz":
    b(s+i)
    if s[-1]==chr(ord(i)-1):return
b("a")
ans.sort()
for i in ans:print(i)

Submission status: Wrong Answer

Solution code:
n=int(input())
for i in range(2**(n-1)):
  S=format(i,'10b')
  A=[1]
  for i in range(n-1):
    if S[-n+i+1]=="1":
      A.append(A[-1]+1)
    else:
      A.append(A[-1])
  for i in range(n):
    print(chr(A[i]+96),end="")
  print()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
n=INT()
l=["aaaa","aaab","aaba","aabb","abaa","abab","abbb","baaa","baab","babb"]
if n>=4:
    for i in range(n):
        print("a"*(n-4)+l[i])
    exit()
if n==1:
    print("a")
    exit()
if n==2:
    print("aa")
    print("ab")
    exit()
if n==3:
    print("aaa")
    print("aab")
    print("aba")
# ls=list("abcdefghijklmn")
# for i in range(n):
#     print("a"*(n-1)+ls[i])

Submission status: Wrong Answer

Solution code:
from itertools import product
N=int(input())
s="abcdefghij"
for i in range(N):
  c=s[:i+1]
  for p in product(c,repeat=N-i):
    print(c+"".join(p))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
from collections import deque, Counter
def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math

def res(n, cur, ln, k):
    if ln == n:
        print(cur)
        return
    for i in range(k+2):
        res(n, cur + chr(ord("a") + i), ln+1, i)
    # res(n, cur + chr(ord("a") + k+1), ln + 1, k+1)


def main():
    n = getN()
    res(n, "a", 1, 0)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
import string
A = [chr(i) for i in range(ord("a"),ord("a")+10)]
B = {}
for i in range(10):
    B[A[i]] = i

def d(s,ANS):
    if len(s) == n:
        ANS.append(s)
    else:
        t = 0 if len(s)==0 else B[s[-1]] + 1
        for i in range(t+1):
            d(s + A[i],ANS)
ANS = []

d("",ANS)

ANS.sort()
for i in ANS:
    print(i)

Submission status: Wrong Answer

Solution code:
s = 'abcdefghij'
sg = s.__getitem__


def dfs(ans):
    if len(ans) == n:
        print(''.join(map(sg, ans)))
        return
    for i in range(ans[-1] + 2):
        ans.append(i)
        dfs(ans)
        ans.pop()


n = int(input())
dfs([0])

Submission status: Wrong Answer

Solution code:
def main():
  n = int(input())
  li  = []
  li.append(["a"])
  for i in range(9):
    x = li[i]
    y = []
    for j in x:
      y.append(j + "a")
      y.append(j + "b")  
    y.sort()
    li.append(y)
  ans = li[n-1]
  for i in ans:
    print(i)  

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
def resolve():
    n = int(input())
    s = []
    if 0<n:
        s.append("a"*n)
    if 1<n:
        s.append("a"+"b"*(n-1))
    if 2<n:
        s.append("ab"+"c"*(n-2))
    if 3<n:
        s.append("abc"+"d"*(n-3))
    if 4<n:
        s.append("abcd"+"e"*(n-4))
    if 5<n:
        s.append("abcde"+"f"*(n-5))
    if 6<n:
        s.append("abcdef" + "g" * (n - 6))
    if 7<n:
        s.append("abcdefg" + "h" * (n - 7))
    if 8<n:
        s.append("abcdefgh"+"i"*(n-8))
    if 9<n:
        s.append("abcdefghi" + "j" * (n - 9))
    print("\n".join(s))

resolve()

Submission status: Wrong Answer

Solution code:
n=int(input())
def b(s):
  if len(s)==n:print(s);return
  for i in "abcdefghijklmnopqrstuvwxyz":
    b(s+i)
    if s[-1]==chr(ord(i)-1):return
b("a")

Submission status: Wrong Answer

Solution code:
def resolve():
    n = int(input())
    s = []
    if 0<n:
        s.append("a"*n)
    if 1<n:
        s.append("a"+"b"*(n-1))
    if 2<n:
        s.append("ab"+"c"*(n-2))
    if 3<n:
        s.append("abc"+"d"*(n-3))
    if 4<n:
        s.append("abcd"+"e"*(n-4))
    if 5<n:
        s.append("abcde"+"f"*(n-5))
    if 6<n:
        s.append("abcdef" + "g" * (n - 6))
    if 7<n:
        s.append("abcdefg" + "h" * (n - 7))
    if 8<n:
        s.append("abcdefgh"+"i"*(n-8))
    if 9<n:
        s.append("abcdefghi" + "j" * (n - 9))
    print("\n".join(s))

Submission status: Wrong Answer

Solution code:
n = int(input())
import string
A = [chr(i) for i in range(ord("a"),ord("a")+10)]
B = {}
for i in range(10):
    B[A[i]] = i

def d(s):
    if len(s) == n:
        print(s)
    else:
        t = 0 if len(s)==0 else B[s[-1]] + 1
        for i in range(t+1):
            d(s + A[i])

d("")

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
x = list(product([0, 1], repeat=N))
ans = []
for i in range(N):
	s = list(x[i])
	for j in range(N):
		if s[j] == 0:
			s[j] = "a"
		else:
			s[j] = "b"
	ans.append("".join(s))

print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
alpha_small_ = [chr(i) for i in range(97, 97+26)] #アルファベッドの小文字
alpha_small_dict = {chr(i):i-97 for i in range(97, 97+26)} #辞書ver

n = int(input())
data = [[] for _ in range(n)]
data[0] = ["a"]
for i in range(1,n):
    for x in data[i-1]:
        for j in alpha_small_[:i+1]:
            data[i].append(x+j)

for x in data[n-1]:
    print(x)

Submission status: Wrong Answer

Solution code:
base='bcdefghij'
n=int(input())
ans=[]
for i in range(1,n+1):
  tmp='a'*i+base[:n-i]
  ans.append(tmp)
ans=sorted(ans)
for s in ans:
  print(s)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
from collections import deque, Counter
def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math

def res(n, cur, ln, k):
    if ln == n:
        print(cur)
        return
    res(n, cur + chr(ord("a") + k), ln+1, k)
    res(n, cur + chr(ord("a") + k+1), ln + 1, k+1)


def main():
    n = getN()
    res(n, "a", 1, 0)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
def main():
    N = int( input())
    if N == 1:
        print('a')
        return
    ANS = ['a']
    for _ in range(N-1):
        G = deepcopy(ANS)
        ANS = []
        for t in G:
            ANS.append(t+'a')
            ANS.append(t+'b')
    print('\n'.join(ANS))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())

stack = [(0, [])]
answers = []
while stack:
    total, digits = stack.pop()
    if total == n:
        answer = []
        is_a = True
        for digit in digits:
            if is_a:
                answer.append('a' * digit)
            else:
                answer.append('b' * digit)
            is_a = not is_a
        answers.append(''.join(answer))

    if total < n:
        for digit in range(1, n - total + 1):
            nxts = list(digits)
            nxts.append(digit)
            stack.append((total + digit, nxts))

answers.sort()
for answer in answers:
    print(answer)

Submission status: Wrong Answer

Solution code:
n = int(input())

def f(frm, l, head):
    if l == 0:
        print(head)
    for i in range(l, 0, -1):
        f(frm + 1, l - i, head + chr(ord('a') + frm) * i)
    
f(0, n, '')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)


N = int(input())
L = list("abcdefghij")

def nasu(x, n, e):
  if n == N:
    print(x)
    return
  for i in range(e + 1):
    if i == n:
      nasu(x + L[i], n + 1, e + 1)
    else:
      nasu(x + L[i], n + 1, e)

nasu("", 0, 0)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
n=INT()
ls=list("abcdefghijklmn")
for i in range(n):
    print("a"*(n-1)+ls[i])

Submission status: Time Limit Exceeded

Solution code:

def main():
	n  = int(input())
	ans = []
	queue=['a']
	while len(queue)>0:
		now = queue.pop(0)
		if len(now)==n:
			print(now)
			continue
		mx=now[0]
		for i in range(0,len(now)):
			mx = max(mx, now[i])
		for i in range(ord('a'), ord(mx)+2):
			t=now+chr(i)
			queue.append(t)
if __name__=="__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:

def main():
	n  = int(input())
	ans = []
	queue=['a']
	while len(queue)>0:
		now = queue.pop(0)
		if len(now)==n:
			ans.append(now)
			continue
		mx=now[0]
		for i in range(0,len(now)):
			mx = max(mx, now[i])
		for i in range(ord('a'), ord(mx)+2):
			t=now+chr(i)
			queue.append(t)
	ans.sort()
	for i in ans:
		print(i)
if __name__=="__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
def main():
	n  = int(input())
	ans = []
	queue=[]
	queue.append('a')
	while len(queue)>0:
		now = queue.pop(0)
		if now in ans:
			continue
		if len(now)==n:
			ans.append(now)
			continue
		for i in range(0,len(now)):
			lst = now[i]
			diff = now + chr(ord(lst)-ord('a')+1+ord('a'))
			same = now+chr(ord(lst))
			queue.append(diff)
			queue.append(same)
	ans.sort()
	for i in ans:
		print(i)
if __name__=="__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
def dfs(n):
    adj = [chr(i+97) for i in range(26)]
    
    stack = ['a']
    while stack:
        node = stack.pop(0)
        if len(node) == n:
            print(node)
        else:
            limit = ord(max(node))%97+2
            for child in adj[:limit]:
                stack.append(node+child)
    return -1

def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    dfs(n)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def dfs(n):
    adj = [chr(i+97) for i in range(26)]
    
    stack = ['a']
    combs = []
    while stack:
        node = stack.pop(0)
        if len(node) == n:
            combs.append(node)
        else:
            limit = ord(max(node))%97+2
            for child in adj[:limit]:
                cand = node+child
                stack.append(node+child)
    return combs

def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    res = dfs(n)
    for ans in res:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

#文字数分aで埋める
#末尾をbにする
#一桁上の文字+1文字までは許容するルールで増加させていく

import itertools

stop_combination = [chr(i) for i in range(97,97+N)]
combination_list = list(itertools.product(stop_combination, repeat=N))

printed_expression = set()

for combination in combination_list:
    #print(combination,":",end="")

    if list(combination) == stop_combination:
        print(''.join(map(str, combination)))
        break

    character_dict = dict()
    character_number = 0
    alternative_expression = ""

    for character in combination:
        if character not in character_dict:
            character_dict[character] = character_number
            alternative_expression += str(character_number)
            character_number += 1
        else:
            alternative_expression += str(character_dict[character])

    #print(alternative_expression)

    if alternative_expression not in printed_expression:
        print(''.join(map(str, combination)))
        printed_expression.add(alternative_expression)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

N = int(input())
added = set()
ans = []
a = [[chr(97+i) for i in range(j)] for j in range(1,N+1)]
for x in product(*a):
    appeared = set('a')
    d = ['a']
    temp = [0]
    for y in x[1:]:
        if y not in appeared:
            appeared.add(y)
            d.append(y)
            temp.append(d.index(y))
        else:
            temp.append(d.index(y))
    t = tuple(temp)
    if t not in added:
        print(''.join(x))
        added.add(t)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

N = int(input())
added = set()
ans = []
a = [[i for i in range(j)] for j in range(1,N+1)]
for x in product(*a):
    count = 0
    appeared = set()
    temp = []
    order = [float('inf')] * N
    for y in x:
        if y not in appeared:
            order[y] = 97 + count
            count += 1
            appeared.add(y)
            temp.append(chr(order[y]))
        else:
            temp.append(chr(order[y]))
    s = ''.join(temp)
    if s not in added:
        ans.append(s)
        added.add(s)
print('\n'.join(ans))

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

N = int(input())
added = set()
ans = []
a = [[i for i in range(j)] for j in range(1,N+1)]
for x in product(*a):
    count = 0
    appeared = set()
    temp = []
    order = [float('inf')] * N
    for y in x:
        if y not in appeared:
            order[y] = 97 + count
            count += 1
            appeared.add(y)
            temp.append(chr(order[y]))
        else:
            temp.append(chr(order[y]))
    s = ''.join(temp)
    if s not in added:
        ans.append(s)
        added.add(s)
print('\n'.join(sorted(ans)))

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

N = int(input())
added = set()
ans = []
a = [[i for i in range(j)] for j in range(1,N+1)]
for x in product(*a):
    count = 0
    appeared = set()
    temp = []
    order = [float('inf')] * N
    for y in x:
        if y not in appeared:
            order[y] = 97 + count
            count += 1
            appeared.add(y)
            temp.append(chr(order[y]))
        else:
            temp.append(chr(order[y]))
    s = ''.join(temp)
    if s not in added:
        ans.append(s)
        added.add(s)
for x in sorted(ans):
    print(x)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect

def main():
    N = int(input())
    letterset = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
    check = []
    for i in range(1, N+1):
        iters = itertools.product(letterset[:i], repeat=N)
        for tmp in iters:
            if len(set(tmp)) == i:
                flag = True
                if tmp[0] != 'a':
                    flag = False
                for k in range(N-1):
                    if not tmp[k+1] in letterset[:len(set(tmp[:k+1]))+1]:
                        flag = False
                if flag:
                    check.append(''.join(tmp))
    check.sort()
    for ans in check:
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
res = []
table = ['a']

def dfs(s, time, table):
    tab = copy.deepcopy(table)
    mi = ord(s[0])
    for i in range(time):
        if mi < ord(s[i]):
            mi = ord(s[i])
            if s[i] in tab:
                 pass
            else:
                tab.append(s[i])
    mi += 1
    l = len(tab)
    if time == n:
        #print(tab)
        if s in res:
            pass
        else:
            print(s)
            res.append(s)
    else:
        for i in range(l):
            dfs(s+tab[i], time+1, tab)
        dfs(s+chr(mi), time+1, tab)
            
dfs('a', 1, table)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
res = []
table = ['a']

def dfs(s, time, table):
    tab = copy.deepcopy(table)
    mi = ord(s[0])
    for i in range(time):
        if mi < ord(s[i]):
            mi = ord(s[i])
            if s[i] in tab:
                 pass
            else:
                tab.append(s[i])
    mi += 1
    
    if time == n:
        #print(tab)
        if s in res:
            pass
        else:
            print(s)
            res.append(s)
    else:
        for i in range(len(tab)):
            dfs(s+tab[i], time+1, tab)
        dfs(s+chr(mi), time+1, tab)
            
dfs('a', 1, table)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
res = []
table = ['a']

def dfs(s, time, table):
    tab = copy.deepcopy(table)
    mi = ord(s[0])
    for i in range(time):
        if mi < ord(s[i]):
            mi = ord(s[i])
            if s[i] in tab:
                 pass
            else:
                tab.append(s[i])
    mi += 1
    
    if time == n:
        #print(tab)
        if s in res:
            pass
        else:
            res.append(s)
    else:
        dfs(s+chr(mi), time+1, tab)
        for i in range(len(tab)):
            dfs(s+tab[i], time+1, tab)
            
dfs('a', 1, table)

#print(res)
res.sort()
#res.reverse()
for i in range(len(res)):
    print(res[i])

Submission status: Time Limit Exceeded

Solution code:
n = str(input())
num2alpha = lambda c: chr(c+97)

def plus(string):
    tmp = []
    for i in range(0,min(int(n)+1,max_value(string)+2)):
        tmp.append(string+str(i))
    return tmp

def max_value(string):
    tmp = []
    for a in list(string):
        tmp.append(int(a))
    return max(tmp)

if n == "1":
    print("a")
else:
    ans = ["0"]
    for i in range(int(n)-1):
        for j,a in enumerate(ans):
            ans[j] = plus(a)
        ans = sum(ans,[])
    for i in ans:
        for j in list(i):
            print(num2alpha(int(j)),end="")
        print()

Submission status: Time Limit Exceeded

Solution code:
import itertools
alp = [chr(ord('a') + i) for i in range(26)]
N = int(input())
allS = list(itertools.product(alp[0:N], repeat=N))
answers = []
PatternList = []
for s in allS:
    pattern = []
    letters = []
    num = 0
    for l in s:
        if not(l in letters):
            letters.append(l)
        pattern.append(letters.index(l))
    if not(pattern in PatternList):
        PatternList.append(pattern)
        answers.append(s)
for answer in answers:
    print(''.join(answer))

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

def func(remain):
    if remain == 1:
        return ['a']
    arr = [chr(ord('a') + i) for i in range(remain)]
    tmp = func(remain - 1)
    res = []
    for i in range(remain):
        res.extend(deepcopy(tmp))
        for j in range(i * len(tmp), (i + 1) * len(tmp)):
            if len(set(res[j])) < i:
                continue
            res[j] += arr[i]
    i = 0
    while i < len(res):
        if len(res[i]) < remain:
            del res[i]
        else:
            i += 1
    return res

n = INT()
ans = func(n)
ans.sort()
for i in range(len(ans)):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n=int(input())
a_num = ord("a")
deque = deque()
deque.append(("a", a_num))
ans = []

while len(deque) > 0:
    s, s_num = deque.popleft()
    for i in range(a_num, s_num + 2):
        s_nxt = s + chr(i)
        s_num = max(s_num, i)
        if len(s_nxt) == n:
            ans.append(s_nxt)
        else:
            deque.appendleft((s_nxt, s_num))

ans.sort()
print(" ".join(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n=int(input())
a_num = ord("a")
deque = deque()
deque.append(("a", a_num))
ans = []

while len(deque) > 0:
    s, s_num = deque.popleft()
    for i in range(a_num, s_num + 2):
        s_nxt = s + chr(i)
        s_num = max(s_num, i)
        if len(s_nxt) == n:
            ans.append(s_nxt)
        else:
            deque.appendleft((s_nxt, s_num))

ans.sort()
for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
l = list(range(N))
lst = []
for i in itertools.product(l,repeat = N):
    _max = i[0]
    if i[0] != 0:
        break
    flg = True
    for j in range(len(i)-1):
        _max = max(_max, i[j])
        if i[j+1] > _max + 1:
            flg = False
            break
    if flg:
        ll = []
        for j in i:
            ll.append(chr(ord('a')+j))
        print("".join(ll))

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
l = list(range(N))
lst = []
for i in itertools.product(l,repeat = N):
    _max = i[0]
    if i[0] != 0:
        break
    flg = True
    for j in range(len(i)-1):
        _max = max(_max, i[j])
        if i[j+1] > _max + 1:
            flg = False
            break
    if flg:
        lst.append(i)
for i in lst:
    for j in i:
        print(chr(ord('a')+j),end="")
    print("")

Submission status: Time Limit Exceeded

Solution code:
# import bisect
# # import heapq
# # from copy import deepcopy
# # from collections import deque
# # from collections import Counter
# # from itertools import accumulate
# # from itertools import permutations
# import numpy as np
# # import math
# a,b,c = map(int, input().split())
n = int(input())
# p = list(map(int, input().split()))
lis = []

def dfs(s, var):
    if len(s) == n and s not in lis:
        lis.append(s)
    else:
        for i in range(var):
            dfs(s+chr(ord('a')+i), var)

        dfs(s+chr(ord('a')+var), var+1)


dfs('a', 1)

for i in range(len(lis)):
    print(lis[i])

Submission status: Time Limit Exceeded

Solution code:
# import bisect
# # import heapq
# # from copy import deepcopy
# # from collections import deque
# # from collections import Counter
# # from itertools import accumulate
# # from itertools import permutations
# import numpy as np
# # import math
# a,b,c = map(int, input().split())
n = int(input())
# p = list(map(int, input().split()))
lis = []

def dfs(s, var):
    if len(s) == n and s not in lis:
        lis.append(s)
    else:
        for i in range(var):
            dfs(s+chr(ord('a')+i), var)

        dfs(s+chr(ord('a')+var), var+1)


dfs('a', 1)
lis.sort()
for i in range(len(lis)):
    print(lis[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lst = ['a','b','c','d','e','f','g','h','i','j']

numsList = list()
memo = list()

def dp(words,nums,c):
    if nums in memo:
        return
    if len(words) == N:
        if nums not in numsList:
            print(words)
            numsList.append(nums)
        return
    for v in range(N):
        if lst[v] not in words:
            dp(words+lst[v],nums + str(c + 1),c+1)
        else:
            dp(words+lst[v],nums + nums[words.find(lst[v])],c)
    memo.append(nums)
dp('a','0',0)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lst = ['a','b','c','d','e','f','g','h','i','j']

numsList = list()
memo = list()

def dp(words,nums,c):
    if words in memo:
        return
    if len(words) == N:
        if nums not in numsList:
            print(words)
            numsList.append(nums)
        return
    for v in range(N):
        if lst[v] not in words:
            dp(words+lst[v],nums + str(c + 1),c+1)
        else:
            dp(words+lst[v],nums + nums[words.find(lst[v])],c)
    memo.append(words)
dp('a','0',0)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lst = ['a','b','c','d','e','f','g','h','i','j']

numsList = list()

def dp(words,nums,c):
    if len(words) == N:
        if nums not in numsList:
            print(words)
            numsList.append(nums)
        return
    for v in range(N):
        if lst[v] not in words:
            dp(words+lst[v],nums + str(c + 1),c+1)
        else:
            dp(words+lst[v],nums + nums[words.find(lst[v])],c)

dp('a','0',0)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lst = ['a','b','c','d','e','f','g','h','i','j']

wordsList = list()
numsList = list()

def dp(words,nums,c):
    if len(words) == N:
        if nums not in numsList:
            wordsList.append(words)
            numsList.append(nums)
        return
    for v in range(N):
        if lst[v] not in words:
            dp(words+lst[v],nums + str(c + 1),c+1)
        else:
            dp(words+lst[v],nums + nums[words.find(lst[v])],c)

dp('a','0',0)

for ans in wordsList:
    print(ans)