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

Solution (Python):
def gen_pattern(N, pre=""):
  for c in range(0x61, ord(max(pre or "\x60"))+2):
    pat = pre+chr(c)
    if len(pat) == N:
      yield pat
    else:
      yield from gen_pattern(N, pat)

for pattern in gen_pattern(int(input())):
  print(pattern)

Solution (Python):
from string import ascii_lowercase as alph

def gen_pattern(N, pre=""):
  for c in alph[:ord(max(pre or "\x60"))-0x5f]:
    pattern = pre+c
    if len(pattern) == N:
      yield pattern
    else:
      yield from gen_pattern(N, pattern)

for pattern in gen_pattern(int(input())):
  print(pattern)

Solution (Python):
from string import ascii_lowercase as alph

def gen_pattern(N, pre="", m=-1):
  for i in range(m+2):
    pat = pre+alph[i]
    if len(pat) == N:
      yield pat
    else:
      yield from gen_pattern(N, pat, max(i,m))

for pattern in gen_pattern(int(input())):
  print(pattern)

Solution (Python):
from string import ascii_lowercase as alph

def gen_pattern(N, pre="", m=-1):
  for i, c in enumerate(alph[:m+2]):
    pat = pre+c
    if len(pat) == N:
      yield pat
    else:
      yield from gen_pattern(N, pat, max(i,m))

for pattern in gen_pattern(int(input())):
  print(pattern)

Solution (Python):
from string import ascii_lowercase as alph

def gen_pattern(N, pre=""):
  for c in alph[:ord(max(pre or "\x60"))-0x5f]:
    pattern = pre+c
    if len(pattern) == N:
      yield pattern
    else:
      yield from gen_pattern(N, pattern)

for pattern in gen_pattern(int(input())):
  print(pattern)

Solution (Python):
from string import ascii_lowercase as alph

def gen_pattern(N, pre=""):
  for i in range(ord(max(pre or "\x60"))-0x5f):
    pattern = pre+alph[i]
    if len(pattern) == N:
      print(pattern)
    else:
      gen_pattern(N, pattern)

gen_pattern(int(input()))

Solution (Python):
from string import ascii_lowercase as alph
from operator import itemgetter

def gen_pattern(N, pre=[]):
  for i in range(max(pre or [-1])+2):
    indexes = pre+[i]
    if len(indexes) == N:
      print("".join(itemgetter(*indexes)(alph)))
    else:
      gen_pattern(N, indexes)

gen_pattern(int(input()))

Solution (Python):


num = int(input())
alf = ['a','b','c','d','e','f','g','h','i','j','k']
def dfs(s, mx_idx):
    if len(s) == num:
        print(s)
        return
    for i in range(mx_idx+1):
        if i == mx_idx:
            dfs(s+alf[i], mx_idx+1)
        else:
            dfs(s+alf[i], mx_idx)

dfs('', 0)

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

def dfs(s, count):
    if len(s) == N:
        print(s)
        return
    for i in range(count + 1):
        dfs(s+abc[i], count+1 if i == count else count)

dfs('', 0)

Solution (Python):
n = int(input())

if n == 1:
    print('a')
    exit()

def dfs(s, cmax):
    if len(s) == n:
        print(s)
    else:
        for i in range(ord(cmax)-ord('a')+2):
            c = chr(i+ord('a'))
            if ord(c) < ord(cmax):
                dfs(s+c, cmax)
            else:
                dfs(s+c, c)

dfs('a', 'a')

Solution (Python):
n = int(input())

if n == 1:
    print('a')
    exit()

def dfs(s, cmax):
    if len(s) == n:
        print(s)
    else:
        for i in range(ord(cmax)-ord('a')+2):
            c = chr(i+ord('a'))
            if ord(c) < ord(cmax):
                dfs(s+c, cmax)
            else:
                dfs(s+c, c)

dfs('a', 'a')

Solution (Python):
N = int(input())

al = 'abcdefghijk'


def next(digits, kind, cur):
    if digits == N:
        print(cur)
        return
    for i in range(kind+1):
        if i == kind:
            next(digits+1, kind+1, cur+al[i])
        else:
            next(digits+1, kind, cur+al[i])


next(1, 1, 'a')

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
N = int(input())
def f(n,s):
    if n == 0:
        print(s)
        return
    for i in [chr(ord("a") + i) for i in range(N-n+1)]:
        mx = 0
        for j in s:
            mx = max(mx,ord(j))
        if s != "" and ord(i) - mx > 1:
            return 
        f(n-1,s+i)
f(N,"")

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
def dfs(s,mx):
    if len(s) == n:
        print(s)
        return
    for i in range(ord("a"),ord(mx)+1):
        c = chr(i)
        if c == mx:dfs(s+c,chr(ord(mx)+1))
        else:dfs(s+c,mx)
n = int(input())
dfs("","a")

Solution (Python):
n = int(input())
strings = [[] for _ in range(n)]
for i in range(n):
    if not i:
        strings[i].append("a")
        continue
    for s in strings[i-1]:
        for j in range(ord("a"),ord(max(s))+2):
            c = chr(j)
            strings[i].append(s+c)
print(*strings[-1])

Solution (Python):
n = int(input())
strings = [[] for _ in range(n)]
for i in range(n):
    if not i:
        strings[i].append("a")
        continue
    for s in strings[i-1]:
        for j in range(ord("a"),ord(max(s))+2):
            c = chr(j)
            strings[i].append(s+c)
print(*strings[-1])

Solution (Python):
L=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i','j']
L_ind = [i for i in range(10)]
N=int(input())
K=[]
for i in range(N):
    if i==0:
        K.append(str(L_ind[0]))
    else:
        K_ = []
        for j in K:
            for m in range(int(max(set(j)))+2):
                K_.append(j + str(L_ind[m]))
        K = K_
    
for k in K:
    ans = ''
    for i in k:
        ans += L[int(i)]
    print(ans)

Solution (Python):
n = int(input())

alphabet = 'abcdefghij'
s = [-1 for i in range(n)]
lst = [[] for i in range(n)]
lst[0].append(0)
ind = 0
while lst[ind] != [] or ind > 0:
    if ind < n-1 and lst[ind] != []:
        s[ind] = lst[ind].pop()
        for i in range(max(s)+1, -1, -1):
            lst[ind+1].append(i)
        ind += 1
    else:
        while lst[ind] != []:
            s[ind] = lst[ind].pop()
            ans = [alphabet[i] for i in s]
            print(''.join(ans))
        s[ind] = -1
        ind -= 1

Solution (Python):
from collections import deque
n = int(input())

d = deque(['a'])

while d:
    w = d.popleft()
    if len(w) == n:
        print(w)
    else:
        w_m = len(set(w))
        for i in range(w_m + 1):
            d.append(w + chr(ord('a') + i))

Solution (Python):
def dfs(s,x):
    if len(s) == n:
        print(s)
    else:
        for c in range(ord('a'), ord(x) + 1):
            dfs(s + chr(c), chr(ord(x) + 1) if chr(c) == x else x)

n = int(input())
dfs('', 'a')

Solution (Python):
n=int(input())

def dfs(s,mx):
  
  if len(s)==n:
    print(s)
    return
  for i in range(ord("a"),mx+2):
    
    
    dfs(s+chr(i),max(mx,i))

dfs("a",ord("a"))

Solution (Python):
n = int(input())

alphabet = 'abcdefghij'
s = [-1 for i in range(n)]

def dfs(ind, r):
    if ind == n:
        ans = [alphabet[i] for i in s]
        print(''.join(ans))
        return
    for i in range(r):
        s[ind] = i
        dfs(ind+1, max(s)+2)
        s[ind] = -1

dfs(0, 1)

Solution (Python):
s="abcdefghijklmnopqrstuvwxyz"
import sys
sys.setrecursionlimit(10**6)
def dfs(a,n,m):
    if len(a)==n:
        print(a)
        return
    for i in range(m):
        if i==m-1:
            dfs(a+s[i],n,m+1)
        else:
            dfs(a+s[i],n,m)
n=int(input())
dfs("",n,1)

Solution (Python):
alp = [chr(i) for i in range(97, 97+26)]

def dfs(s, max_):
    if len(s)==N:
        print(s)
    else:
        for i in range(max_+1):
            if i==max_:
                dfs(s+alp[i], max_+1)
            else:
                dfs(s+alp[i], max_)
                
        
N = int(input())
dfs('',0)

Solution (Python):
N=int(input())
S="a"
bottom=""
alpha="abcdefghijklmnopqrstuvwxyz"

for i in range(N):
    if i>25:
        bottom=bottom+"z"
    else:
        bottom=bottom+alpha[i]
    S=S+"a"

C=0
S=""
def F(S,loop,N,C):
    for i in range(0,loop):
        if N==C+1:
            print(S+bottom[i])
        else:
            if loop==i+1:
                loop+=1
            F(S+bottom[i],loop,N,C+1)
    return None
            
F(S,1,N,C)

Solution (Python):
n = int(input())
res = [['a'],[],[],[],[],[],[],[],[],[]]
cha = ['a','b','c','d','e','f','g','h','i','j']

for x in range(1,n):
  for y in res[x-1]:
    for c in range(10):
      if cha[c] not in y:
        for d in range(c+1):
          res[x].append(y+cha[d])
        break
  if x > n + 1:
    break

for ans in range(len(res[n-1])):
  print(res[n-1][ans])

Solution (Python):

N = int(input())

s = "abcdedcba" 
answer = ['a'] 


def search_max(s):
    max = 'Z' 
    for i in range(len(s)):
        if(s[i] > max):
            max = s[i]
    return max



def next(p):
    list_alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
    alpha_index = -100
    for i in range(len(list_alpha)):
        if(p == list_alpha[i]):
            alpha_index = i
    
    return list_alpha[:alpha_index+2]




def one_plus(l): 
    ret_list = [] 
    for i in range(len(l)): 
        max_alpha = search_max(l[i]) 
        next_list = next(max_alpha) 
        
        for j in range(len(next_list)):
            ret_list.append(l[i] + next_list[j])
    return ret_list

for i in range(N-1):
    answer = one_plus(answer) 


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

'''
def sum(n):
    if n < 1:
        return n
    return n + sum(n-1)

si = sum(100)
print("1から100の合計は", si, "です")
'''

Solution (Python):
import sys,math
sys.setrecursionlimit(10**9)
import itertools
from collections import defaultdict
 
IA = lambda: map(int,input().split())
IAS= lambda: map(str,input().split())


n=int(input())


def dfs(s,lim):

    
    if len(s)==n:
        print(s)
    else:
        for i in range(ord('a'),ord(lim)+1):
            
            if i==ord(lim):
                dfs(s+chr(i),chr(ord(lim)+1))
            else:
                dfs(s+chr(i),lim)

dfs('','a')

Solution (Python):
n = int(input())
q = [("0",0)]
cnt = 0
while cnt < n - 1:
    qt = []
    for numset in q:
        num = numset[0]
        maxnum = numset[1]
        for i in range(int(maxnum) + 2):
            mx = max(maxnum,i)
            qt += [(num + str(i), mx)]
    q = qt.copy()
    cnt += 1
    
alp = "abcdefghijkl"
    
def show(num):
    ret = ""
    l = len(num)
    for i in range(l):
        ret += alp[int(num[i])]
    return ret

q.sort()

for num in q:
    print(show(num[0]))

Solution (Python):
from collections import deque
import sys
sys.setrecursion = 10000000

n = int(input())
alpha = "abcdefghijklmnopqrstuvwxyz"
def dfs(str,letter):
  if len(str) == n:
    print(str)
  else:
    length = len(set(str))
    for let in range(length+1):
      dfs(str+alpha[let],let)

def solve(): 
  query = deque([])
  dfs("a",1)


solve()

Solution (Python):
N = int(input())
alphabet_list = [chr(i) for i in range(97, 97 + N)]

import collections

previous_list = ["a"]
for i in range(1, N):
    out_list = []
    for a in previous_list:
        
        for al in alphabet_list[:len(collections.Counter(a))+1]:
            
            out_list.append(a+al)
    previous_list = out_list

for out in previous_list:
    print(out)

Solution (Python):
n = int(input())
a1 = list("abcdefghij")

b1 = [[0]]
c1 = 1
ans = []
while c1 < n:
    c1 += 1
    b2 = b1 + []
    b1 = []
    for i in b2:
        imax = max(i)
        for j in range(imax+2):
            b1.append(i+[j])

for i in b1:
    ans = [a1[j] for j in i]
    print("".join(ans))

Solution (Python):
n = int(input())

def dfs(s, cn):
    if len(s) == n:
        print(s)
    else:
        for i in range(0, cn + 1):
            c = chr(ord('a') + i)
            if i == cn: dfs(s+c, cn+1)
            else: dfs(s+c, cn)

dfs('', 0)

Solution (Python):


def dfs(str, max, rast):
    if(rast==0):
        return 0
    else:
        for i in range(ord('a'), max+1):
            if(rast==1):
                print(str + chr(i))
            if(i==max):
                dfs(str+chr(i), max+1, rast-1)
            else:
                dfs(str+chr(i), max, rast-1)


def main():
    n = int(input())
    s = 'a'
    if(n==1):
        print(s)
    dfs(s, ord(s[-1])+1, n-1)

main()

Solution (Python):
N = int(input())
 
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
 
def solve(n):
    if n == 1:
        return ['a']
    elif n == 2:
        return ['aa', 'ab']
    else:
        chars = alphabets[0:n]
        result = []
        for retsu in solve(n - 1):
            done = False
            for char in chars:
                if char in retsu:
                    result.append(retsu + char)
                else:
                    if not done:
                        result.append(retsu + char)
                        done = True
                    else:
                        break
        return result
 
print("\n".join(solve(N)))

Solution (Python):
n = int(input())

alpha = [chr(i) for i in range(97, 97+26)]

def dfs(s, mx):
    if(len(s)==n):
        print(s)
    else:
        for i in range(mx):
            if i==mx-1:
                dfs(s+alpha[i], mx+1)
            else:
                dfs(s+alpha[i], mx)

dfs("",1)

Solution (Python):
N = int(input())
F="abcdefghijklmnopqrstuvwxyz"
def cy(ans,l):
  
  if len(ans)==N:
    print(ans)
  else:
    for i in range( min(l+2,27) ):
      k=ans+F[i]
      cy(k,max(l,i))

cy("a",0)

Solution (Python):
N = int(input())

char = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

def search(arr):
  global N, char
  if len(arr) == N:
    ans = ''.join([char[x] for x in arr])
    print(ans)
    return
  else:
    for i in range(max(arr) + 2):
      arr_next = arr + [i]
      search(arr_next)

search([0])

Solution (Python):
c = 'abcdefghij'

def join(s, k, n):
    if len(s) == n:
        return [s]
    ans = []
    for i in range(k):
        ans += join(s + c[i], k, n)
    ans += join(s + c[k], k + 1, n)
    return ans

n = int(input())
ans = join('', 0, n)
for x in ans:
    print(x)

Solution (Python):
n = int(input())

ans = [0] * n

def prc(L):
    res = []
    for x in L:
        res.append(chr(ord('a') + x))
    print(''.join(res))

while 1:
    prc(ans)
    for i in range(n-1, 0, -1):
        temp = max(ans[:i])
        if ans[i] <= temp:
            ans[i] += 1
            for j in range(i+1, n):
                ans[j] = 0
            break
    else:
        break

Solution (Python):
n = int(input())

def chrinc(s,n):
  return chr(ord(s)+1)

def dfs(s,mx):
  if len(s) == n:
    print(s)
  else:
    c = 'a'
    while c <= mx:
      dfs(s+c, chrinc(mx,1) if c==mx else mx)
      c = chrinc(c,1)

dfs('','a')

Solution (Python):
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 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 = float('inf')
mod = 10 ** 9 + 7

N = INT()
l = "abcdefghijk"

def dfs(cur, mx):
    if len(cur) == N:
        print(cur)
        return
    
    for c in range(len(l[:mx+2])):
        t = cur
        t += l[:mx+2][c]
        
        dfs(t, max(c, mx))
        

dfs("", -1)

Solution (Python):
import collections

N = int(input())

W = ["a"]
nW = []

"""
k = len(collections.Counter(Y[0]))
print(k)
"""
for j in range(N-1):
    for w in W:
        k = len(collections.Counter(w))
        for i in range(k+1):
            nW.append(w + chr(i+97))
    W = nW.copy()
    nW.clear()
    
    


for i in W:
    print(i)

Solution (Python):
import itertools as iters

def enlong(s,m):
    return [s + [i] for i in range(1,m+2)]

N = int(input())
alpha = "abcdefghijk"
R = [[1]]
for _ in range(N-1):
    S = []
    for r in R:
        S += enlong(r,max(r))
    R = S[::]
for r in R:
    s = "".join([alpha[ss-1] for ss in r])
    print(s)

Solution (Python):
n = int(input())

lets = [chr(i) for i in range(ord("a"), ord("z") + 1)]
ans = []


def dfs(s, l, mx):
    if l == n:
        ans.append(s)
        return

    for i in range(mx + 2):
        dfs(s + lets[i], l + 1, max(mx, i))


dfs("", 0, -1)
print(*ans, sep="\n")

Solution (Python):
def f(num,N,S):
    alphabet = ["a","b","c","d","e","f","g","h","i","j"]
    if N == num:
        for i in range(len(S)):
            print(S[i])
        return
    else:
        tmp = []
        length = len(S)
        for i in range(length):
            ttmp = sorted(list(S[i]))
            for j in range(alphabet.index(ttmp[-1])+2):
                tmp.append(S[i]+alphabet[j])
        S = tmp
        f(num+1,N,S)
 
N = int(input())
f(1,N,["a"])

Solution (Python):


import sys
sys.setrecursionlimit(200000)



N = int(input())
l = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]


def solve(s, m):
    if len(s) == N:
        print(s)
    else:
        for i in range(m + 1):
            solve(s + l[i], max(i + 1, m))


def main():
    solve("a", 1)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
def mojiOut(s, mx):
    if len(s) == N:
        print(s)
        return

    for i in range(ord("a"), ord(mx) + 2):
        strS = s
        strS += chr(i)
        mojiOut(strS, max(mx, chr(i)))

mojiOut("a",  "a")

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()

ans = set()
def rec(i, S):
    if S in ans:
        return
    if i == N:
        ans.add(S)
        return
    
    for c in S:
        rec(i+1, S+c)
    
    rec(i+1, S+chr(ord(max(S))+1))
rec(1, 'a')
ans = sorted(ans)
[print(s) for s in ans]

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()

ans = set()
def rec(i, S):
    if S in ans:
        return
    if i == N:
        ans.add(S)
        return
    
    for c in S:
        rec(i+1, S+c)
    
    rec(i+1, S+chr(ord(max(S))+1))
rec(1, 'a')
ans = sorted(ans)
[print(s) for s in ans]

Solution (Python):
n = int(input())

def dfs(s, mx):
    if len(s) == n:
        print(s)
    else:
        for i in range(ord("a"), ord(mx) + 2):
            tmp = s
            tmp += chr(i)
            dfs(tmp, max(mx, chr(i)))

dfs("a", "a")

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()

ans = set()
def rec(i, S):
    if S in ans:
        return
    if i == N:
        ans.add(S)
        return
    
    for c in S:
        rec(i+1, S+c)
    
    li = sorted(S)
    rec(i+1, S+chr(ord(li[-1])+1))
rec(1, 'a')
ans = sorted(ans)
[print(s) for s in ans]

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()

ans = set()
def rec(i, S):
    if S in ans:
        return
    if i == N:
        ans.add(S)
        return
    
    for c in S:
        rec(i+1, S+c)
    
    li = sorted(S)
    rec(i+1, S+chr(ord(li[-1])+1))
rec(1, 'a')
ans = sorted(ans)
[print(s) for s in ans]

Solution (Python):

def bfs(s):
    if len(s) == n:
        print(s)
        return 
    ord_num = 97
    while True:
        
        char = chr(ord_num)
        if char in s:
            bfs(s+char)
        else:
            bfs(s+char)
            break
        ord_num += 1
if __name__ == "__main__":
    n = int(input())
    s = ""
    bfs(s)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
alpha = "abcdefghij"
ans = []
N = int(input())

def dfs(S, num):
    if len(S) == N:
        ans.append(S)
    else:
        right = num + 2
        seq = alpha[:right]
        for i, c in enumerate(seq):
            dfs(S+c, max(i, num))

dfs("a", 0)
ans = sorted(ans)
for a in ans:
    print(a)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
alpha = "abcdefghij"
ans = []
N = int(input())

def dfs(S, num):
    if len(S) == N:
        ans.append(S)
    else:
        right = num + 2
        seq = alpha[:right]
        for i, c in enumerate(seq):
            dfs(S+c, max(i, num))

dfs("a", 0)
ans = sorted(ans)
for a in ans:
    print(a)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
alpha = "abcdefghij"
ans = []
N = int(input())

def get_right(S):
    alpha_idx = 0
    for i in range(len(S)):
        flag = S.find(alpha[i])
        if flag > 0:
            alpha_idx = i
    return alpha_idx + 2


def dfs(S, num):
    if len(S) == N:
        ans.append(S)
    else:
        right = num + 2
        seq = alpha[:right]
        for i, c in enumerate(seq):
            dfs(S+c, max(i, num))

dfs("a", 0)
ans = sorted(ans)
for a in ans:
    print(a)

Solution (Python):
N = int(input())
ans = ["a"]
for i in range(N-1):
    new = []
    for j in range(len(ans)):
        l = ord(max(ans[j]))-95
        add = [chr(ord("a") + idx) for idx in range(l)]
        for k in add :
            new.append(ans[j] + k)
    ans = new

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

Solution (Python):
import sys

input = sys.stdin.buffer.readline







def main():
	n=int(input())
	s="abcdefghijklmn"

	li=[[] for _ in range(10)]
	li[0]=[["a",0]]


	for i in range(1,10):

		for t,val in li[i-1]:
			for j in range(val+2):
				li[i].append([t+s[j],max(val,j)])

	for t,_ in li[n-1]:
		print(t)


if __name__ == "__main__":
	main()

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
alpha = "abcdefghij"
ans = []
N = int(input())

def get_right(S):
    alpha_idx = 0
    for i in range(len(S)):
        flag = S.find(alpha[i])
        if flag > 0:
            alpha_idx = i
    return alpha_idx + 2


def dfs(S):
    if len(S) == N:

        ans.append(S)
    else:
        right = get_right(S)
        seq = alpha[:right]
        for c in seq:
            dfs(S+c)
    return

dfs("a")
ans = sorted(ans)
for a in ans:
    print(a)

Solution (Python):
import copy
n = int(input())

def dfs(str_list, now):
    if len(str_list) == n:
        print(''.join(str_list))
    else:
        chr_a = ord('a')
        for c in range(chr_a, now + 2, 1):
            str_list_cp = copy.deepcopy(str_list)
            str_list_cp.append(chr(c))
            dfs(str_list_cp, max(c, now))

dfs([], ord('a') - 1)

Solution (Python):
n = int(input())

def normal_form(x):
    if x == 1:
        return ['a']
    temp = normal_form(x-1)
    res = []
    for s in temp:
        for k in range(ord(max(s))-97+2):
            res.append(s+chr(k+97))
    return res

print('\n'.join(normal_form(n)))

Solution (Python):
n=int(input())
abc='abcdefghij'
def dfs(i,a,s):
    if i==n:
        print(s)
    else:
        for j in range(0,a+1):
            dfs(i+1,max(a,j+1),s+abc[j])
dfs(0,0,'')

Solution (Python):
import string

N = int(input())
alph = string.ascii_lowercase

def search(S, mx):
    if len(S) == N:
        yield S
    else:
        for s in alph[: mx]:
            for w in search(S + s, mx):
                yield w
        for w in search(S + alph[mx], mx + 1):
            yield w

ans = list(search('', 0))
print(*ans, sep='\n')

Solution (Python):
import math


data = [ "a","b","c","d","e","f","g","h","i","j" ]


result = []


def create( max_letter, remaining_depth, text ):
    if remaining_depth == 0:
        result.append(text)
        return
    
    for i in range(0,max_letter+1):
        new_text = text + data[i]
        create( max(i+1,max_letter),remaining_depth-1,new_text )
        
    
        
        





N = int(input())

create( 0,N,"")

result.sort()
for line in result:
    print(line)

Solution (Python):
def dfs(t, c_max):
    if(len(t)==n):
        print(t)
    else:
        for i in range(c_max+1):
            tmp_t = t
            tmp_t += s[i]
            
            dfs(tmp_t,max(i+1, c_max))

s = "abcdefghijklm"
n = int(input())

dfs('a', 1)

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

N = int(input())
alph = string.ascii_lowercase

def search(S, mx):
    if len(S) == N:
        print(S)
    else:
        for s in alph[: mx + 1]:
            search(S + s, mx)
        search(S + alph[mx + 1], mx + 1)

search('a', 0)

Solution (Python):
n = int(input())

s = ['' for i in range(n)]
def dfs(k, c):
    if k == n:
        print("".join(s))
        return
    for i in range(c):
        s[k] = chr(i+ord('a'))
        dfs(k+1, c)
    s[k] = chr(c+ord('a'))
    dfs(k+1, c+1)

dfs(0, 0)