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):
n = int(input())
ans = []
start = "a"
stack = []
stack.append(start)
num = ord("a")
while stack:
    v = stack.pop()
    if len(v) == n:
        ans.append(v)
        continue
    s = list(v[::])
    s.sort()
    nex_num = ord(s[-1])
    for i in range(num, nex_num + 2):
        tuika = chr(i)
        stack.append(v + tuika)
print(*ans[::-1], sep="\n")

Solution (Python):
n = int(input())
ans = []
start = ("a", 1, ord("a"))
stack = []
stack.append(start)
num = ord("a")
while stack:
    v, nagasa, mx = stack.pop()
    if nagasa == n:
        ans.append(v)
        continue
    for i in range(num, mx + 2):
        tuika = chr(i)
        mx = max(i, mx)
        stack.append((v + tuika, nagasa + 1, mx))
print(*ans[::-1], sep="\n")

Solution (Python):
n = int(input())
ans = []
start = "a"
stack = []
stack.append(start)
num = ord("a")
while stack:
    v = stack.pop()
    if len(v) == n:
        ans.append(v)
        continue
    s = list(v[::])
    s.sort()
    nex_num = ord(s[-1])
    for i in range(num, nex_num + 2):
        tuika = chr(i)
        stack.append(v + tuika)
ans.sort()
print(*ans, sep="\n")

Solution (Python):
def dfs(i,sent):
    if len(sent)==N:
        print(sent)
        return
    if i>N:
        return
    for j in range(i+1):
        dfs(max(i,j+1),sent+chara[j])
    return

N=int(input())
chara=["a","b","c","d","e","f","g","h","i","j"]
chara=chara[:N]
ans=[]
dfs(1,"a")

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

def dfs(rootlist,deep,n):
    global nag
    if len(rootlist) != nag:
        for i in range(n+1):
            nextroot = rootlist[:]
            nextroot.append(i)
            if i < n:
                dfs(nextroot,deep+1,n)
            else:
                dfs(nextroot,deep+1,n+1)
    else:
        answer = ''
        wordlist = ['a','b','c','d','e','f','g','h','i','j']
        for index in rootlist:
            answer += wordlist[index]
        print(answer)


dfs([0],0,1)

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

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

Solution (Python):



import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
from itertools import combinations
input = sys.stdin.readline




n = int(input())
if n == 1:
    print('a')
    sys.exit()


def dfs(s):
    if len(s) == n:
        print(''.join([chr(s + ord('a')) for s in s]))
    else:
        for i in range(len(set(list(s))) + 1):
            dfs(s + [i])
dfs([])

Solution (Python):
n = int(input())
def dfs(i,s):
    if i == n:
        print(s)
        return
    ma = 0
    for j in s:
        ma = max(ma,ord(j))
    for j in range(ma - 95):
        dfs(i+1,s+chr(97+j))
    return
dfs(1,"a")

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

lis=[[[1,"a"]]]

for i in range(n-1):
    lisi=[]
    for j in lis[i]:
        for k in range(j[0]):
            lisi.append([j[0],j[1]+chr(ord("a")+k)])
        lisi.append([j[0]+1,j[1]+chr(ord("a")+j[0])])
    lis.append(lisi)

for i in lis[n-1]:
    print(i[1])

Solution (Python):
from collections import deque 
def dfs(n):
    adj = 'abcdefghij'
    
    stack = deque(['a'])
    while stack:
        
        node = stack.popleft()
        if len(node) == n:
            print(node)
        else:
            
            for child in adj[:len(set(node))+1]:
                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()

Solution (Python):
def dfs(s,n,adj):
    adj = 'abcdefghij'
    if len(s) == n:
        print(s)
    else:
        for child in adj[:len(set(s))+1]:
            dfs(s+child,n,adj)
    return -1

def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    adj = 'abcdefghij'
    dfs('a',n,adj)


if __name__ == '__main__':
    main()

Solution (Python):
ans=[]
N=int(input())
def saiki(A):
    eigo="abcdefghijklmnopqrstuvwxyz"
    if len(A)==N:
        B="".join(A)
        ans.append(B)
        return
    for i in range(len(set(A))+1):
        A.append(eigo[i%26])
        saiki(A)
        A.pop()
saiki([])
ans.sort()
for i in ans:
    print(*i,sep="")

Solution (Python):
from itertools import combinations_with_replacement

N = int(input())
C = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
DP = [set() for _ in range(N)]
DP[0].add((1, 'a'))

for i in range(1, N):
    for num, s in DP[i-1]:
        for j in range(num):
            DP[i].add((num, s + C[j]))
        DP[i].add((num + 1, s + C[num]))
A = []
for _, ax in DP[N-1]:
    A.append(ax)
A.sort()
for a in A:
    print(a)

Solution (Python):
n = int(input())
alph = 'abcdefghij'


def dfs(s, i):
    if len(s) == n:
        return s
    return "\n".join([dfs(s + c, i)for c in alph[:i]] +\
                     [dfs(s + alph[i], i + 1)])


print(dfs("a", 1))

Solution (Python):
def dfs(s, ub, n):
    if len(s) == n:
        print(s)
    else:
        ch = "a"
        while ord(ch) < ord(ub):
            dfs(s + ch, ub, n)
            ch = chr(ord(ch) + 1)
        ub = chr(ord(ub) + 1)
        dfs(s + ch, ub, n)

def solve(n):
    dfs("", "a", n)

n = int(input())
solve(n)

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

hyo = [[[1]]]

for i in range(n-1):
    n_hyo = []
    for arr in hyo[i]:
        for j in range(max(arr)+1):
            n_arr = arr[:]
            n_arr.append(j+1)
            n_hyo.append(n_arr)
    hyo.append(n_hyo)

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

for i in hyo[n-1]:
    ans = ''
    for j in i:
        ans += alp[j-1]
    print(ans)

Solution (Python):
N = int(input())
alpha=list('abcdefghijklmnopqrstuvwxyz')
res=['']
for i in range(N):
    tmp = []
    for j in res:
        for k in alpha[:len(set(list(j))) +1]:
            tmp.append(j+k)
    res = tmp

for i in res:
    print(i)

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

l = []


def f(n):
    return chr(n+ord("a"))


def search(s: str, cnt: int) -> str:
    if len(s) == n:
        return s
    cnt = len(set(list(s)))
    for i in range(0, min(cnt, len(s)) + 1):
        g = search("".join([s, f(i)]), cnt + max(0, i - len(s) + 1))
        if g is not None:
            l.append(g)


search("", 1)

l.sort()

for s in l:
    print(s)

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





ans = [0 for _ in range(N)]
stop = [i for i in range(N)]

def ans_print(ans_list):
    ans = list()
    for a in ans_list:
        ans.append(chr(a+97)) 

    print(''.join(map(str, ans)))
    return 0

def ans_check(ans):
    max_in_ans = 0

    for i in range(1,N):
        if max_in_ans + 1 < ans[i]:
            ans[i-1] += 1
            ans[i] = 0
            return ans_check(ans)
        else:
            max_in_ans = max(max_in_ans,ans[i])
    else:
        return ans

if N == 1:
    print("a")
else:
    while 1:
        ans_check(ans)
        ans_print(ans)
        ans[-1] += 1
        if ans == stop:
            ans_print(ans)
            break

Solution (Python):
N = int(input())
def sub(l, d):
  if d == N:
    s = "".join(["abcdefghijklmnopqrstuvwxyz"[i] for i in l])
    print(s)
    return
  for j in range(max(l)+1+1):
    sub(l+[j], d+1)
sub([0], 1)

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

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

dfs('', 0)

Solution (Python):
from collections import deque
N=int(input())
d=deque()
d.append("a")
dall=deque()
dall.append(d)
i=0
while i<N-1:
    length=len(dall)
    j=0
    while j<length:
        l=dall.popleft()
        llen=len(set(l))
        for k in range(llen+1):
            q = deque()
            for m in range(len(l)):
                c = l.popleft()
                q.append(c)
                l.append(c)
            q.append(chr(ord("a")+k))
            dall.append(q)
        
        
        j=j+1
    i=i+1
    
    

ans=[]
for i in range(len(dall)):
    print("".join(dall[i]))

Solution (Python):
from collections import deque
N=int(input())
d=deque()
d.append("a")
dall=deque()
dall.append(d)
i=0
while i<N-1:
    length=len(dall)
    j=0
    while j<length:
        l=dall.popleft()
        llen=len(set(l))
        for k in range(llen+1):
            q = deque()
            for m in range(len(l)):
                c = l.popleft()
                q.append(c)
                l.append(c)
            q.append(chr(ord("a")+k))
            dall.append(q)
        
        
        j=j+1
    i=i+1
    
    

ans=[]
for i in range(len(dall)):
    ans.append("".join(dall[i]))
ans.sort()
for i in range(len(ans)):
    print(ans[i])

Solution (Python):
al = "abcdefghijklmnopqrstuvwxyz"
k  = int(input())
def dfs(s: "char", i):
    if(len(s)==k):
        print(s)
    else:
        for j in range(i+1):
            if al[j]==al[i]:
                dfs(s+al[j], i+1)
            else:
                dfs(s+al[j], i)




def main():
    
    dfs("", 0)


if __name__ == '__main__':
    main()

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

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

dfs('a', 1)

Solution (Python):
from itertools import combinations

n = int(input())

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


dfs('a')

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

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

Solution (Python):


N = int(input())

alphabet = "abcdefghijklmnopqrstuvwxyz"

curr_str = []

def print_str(curr_str):
    string = ""
    for i in curr_str:
        string += alphabet[i]
    print(string)


def dfs(index, curr_str, curr_char):
    if curr_char > index:
        return None
    if curr_char > max(curr_str) + 1:
        return None

    if index == N-1:
        print_str(curr_str + [curr_char])
        return None

    for i in range(N):
        dfs(index+1, curr_str+[curr_char], i)


if N == 1:
    print("a")
else:    
    for i in range(N):
        dfs(1, [0], i)

Solution (Python):
n = int(input())
alp = [chr(ord("a") + x) for x in range(26)]

def dfs(S):
    if len(S) == n:
        print(S)
    else:
        max_s = ord(max(S)) - 96
        
        for i in range(max_s + 1):
            dfs(S + alp[i])

dfs("a")

Solution (Python):
al=[chr(ord('a') + i) for i in range(26)]

def f(x,c):
    if len(x) == n:
        print(x)
    else:
        for i in range(c):
            f(x+al[i],c)
        f(x+al[c],c+1)

n = int(input())

f("a",1)

Solution (Python):
from collections import deque
N = int(input())
c = [chr(ord("a") + i) for i in range(26)]
q = deque("a")
ans = []
while q:
    s = q.pop()
    if len(s) == N:
        ans.append(s)
        continue
    for x in c[:c.index(max(s)) + 2]:
        q.append(s + x)
[print(a) for a in sorted(ans)]

Solution (Python):
import queue

n = int(input())
alp = "abcdefghijklmn"
q = queue.Queue()
q.put("a")
while not q.empty():
    qi = q.get()
    if len(qi) == n:
        print(qi)
    elif len(qi) < n:
        idx = alp.index(max(qi))
        for i in range(idx+2):
            q.put(qi+alp[i])

Solution (Python):
N = int(input())
answers = []

def cal(already_used, answer):
    if len(answer) == N:
        answers.append(answer)
        return

    if len(already_used) == 0:
        plus_1 = 0
    else:
        plus_1 = max(already_used) + 1

    cur_target = already_used + [plus_1]
    for c in cur_target:
        _answer = answer + str(c)
        if c != plus_1:
            _answer = cal(already_used, _answer)
        else:
            _answer = cal(cur_target, _answer)


cal([], "")
for answer in answers:
    for (num, alp) in zip([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]):
        answer = answer.replace(str(num), alp)
    print(answer)

Solution (Python):
def dfs(s,n):
    if len(s) == n:
        print(s)
    else:
        for i in range(len(set(s))+1):
            dfs(''.join([s,chr(97+i)]),n)

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

Solution (Python):


n = int(input())

def dfs(s, mx, path):
    if n == len(s):
        path.append(s)
        return
    for i in range(ord('a'), mx + 1):
        if i == mx:
            dfs(s + chr(i), mx + 1, path)
        else:
            dfs(s + chr(i), mx, path)

path = []
dfs('', ord('a'), path)
path.sort()
for ans in path:
    print(ans)

Solution (Python):
import sys
from collections import deque
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    if N==1:
        print('a')
        exit()
    d = deque(['a'])
    ans = []
    while d:
        s = d.popleft()
        last = ord(max(s))
        for i in range(97,last+2):
            S = s + chr(i)
            if len(S)==N:
                ans.append(S)
            else:
                d.append(S)
    ans.sort()
    for s in ans:
        print(s)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")











N = int(input())
if N == 1:
    print("a")
else:
    S = list("abcdefghij")
    Q = deque([("a",1)])  
    while len(Q) != 0:
        s,kind = Q.popleft()
        for i in range(kind+1):
            news = s+S[i]
            if len(news) == N+1:
                break
            if len(news) == N:
                print(news)
            if i == kind:
                Q.append((news,kind+1))
            else:
                Q.append((news,kind))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

N = int(input())

lst1 = [('a', 0)]
lst2 = []
for i in range(N - 1):
    if i % 2 == 0: 
        lst2 = []
        for s, t in lst1:
            last = max(ord(s[i]) - ord('a'), t)
            for j in range(last + 2):
                lst2.append((s + chr(j + ord('a')), max(t, j)))
    else:
        lst1 = []
        for s, t in lst2:
            last = max(ord(s[i]) - ord('a'), t)
            for j in range(last + 2):
                lst1.append((s + chr(j + ord('a')), (max(t, j))))
           
if len(lst2) > len(lst1):
    lst2.sort()
    for s, t in lst2:
        print (s)
else:
    lst1.sort()
    for s, t in lst1:
        print (s)

Solution (Python):
def dfs(word, N):
    if len(word) == N:
        print(word)
    else:
        for i in range(len(set(word))+1):
            dfs(word + chr(97+i), N)


def main():
    N = int(input())
    dfs('a', N)


if __name__ == "__main__":
    main()

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

alpha=list('abcdefghijk')

q=deque([])
q.append(alpha[0])

for i in range(1,n):
    qi=deque([])

    while(len(q)>0):
        qij=q.popleft()
        qij_setnum=len(set(list(qij)))
        for j in range(qij_setnum+1):
            qi.append(qij+alpha[j])

    q=qi

q=list(q)
q.sort()
for i in range(len(q)):
    print(q[i])

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
s = ["a"]
ord_a = ord("a")
ans = []

def dfs(s):
    if len(s) == n:
        ans.append("".join(s))
        return   
    max_ord = 0
    for i in s:
        max_ord = max(max_ord, ord(i) - ord_a)
    for j in range(max_ord + 2):
        new_s = s[:]
        new_s.append(chr(j + ord_a))
        dfs(new_s)
if n == 1:
    print("a")
    sys.exit()
dfs(s)
ans.sort()
for i in ans:
    print(i)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
s = ["a"]
ord_a = ord("a")
ans = []
from copy import deepcopy
def dfs(s):
    if len(s) == n:
        ans.append("".join(s))
        return   
    max_ord = 0
    for i in s:
        max_ord = max(max_ord, ord(i) - ord_a)
    for j in range(max_ord + 2):
        new_s = deepcopy(s)
        new_s.append(chr(j + ord_a))
        dfs(new_s)
if n == 1:
    print("a")
    sys.exit()
dfs(s)
ans.sort()
for i in ans:
    print(i)

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

L = [0]*n
for i in range(n):
    L[i] = chr(i+ord('a'))


ans = []
def dfs(A):
    if len(A) == n:
        ans.append(''.join(A))
        return
    for v in L[0:len(set(A))+1]:
        A.append(v)
        dfs(A)
        A.pop()

dfs([])
ans.sort()
for i in range(len(ans)):
    print(ans[i])

Solution (Python):
from collections import *

def dfs(now):
    if len(now)==N:
        print(''.join(now))
        return

    for i in range(ord(max(now))-ord('a')+2):
        now.append(alpha[i])
        dfs(now)
        now.pop()

N = int(input())
alpha = 'abcdefghijklmnopqrstuvwxyz'
dfs(deque(['a']))

Solution (Python):
import string
N = int(input())
alph = string.ascii_lowercase

ans = []
A = [0]
def search():
    if len(A) == N:
        ans.append(tuple(A))
        return

    mx = max(A)
    for i in range(mx + 2):
        A.append(i)
        search()
        A.pop()
    return

search()

for a in ans:
    print(''.join(alph[i] for i in a))

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

def dfs(s, count):
    if len(s) == n:
        print(s)
        return
 
    for z in range(count + 1):
        dfs(s + a[z], count + 1 if z == count else count)

dfs("",0)

Solution (Python):
N=int(input())
def struct(s,i,n,ma):
    if i==n:
        print(s)
    else:
        for j in range(0,ma+2):
            struct(s+chr(ord('a')+j),i+1,n,max(j,ma))
struct('a',1,N,0)

Solution (Python):

import sys
def write(x):
    sys.stdout.write(x)
    sys.stdout.write("\n")
    
from queue import deque
n = int(input())
q = deque([("a", "a")])
while True:
    s, m = q.pop()
    if len(s)==n:
        write(s)
    elif len(s)>=n+1:
        break
    for o in range(ord("a"), ord(m)+2):
        if ord(m)<o:
            m = chr(o)
        q.appendleft((s + chr(o), m))

Solution (Python):

import sys
write = lambda x: sys.stdout.write(x+"\n")
from queue import deque
n = int(input())
q = deque([("a", "a")])
while True:
    s, m = q.pop()
    if len(s)==n:
        write(s)
    elif len(s)>=n+1:
        break
    for o in range(ord("a"), ord(m)+2):
        if ord(m)<o:
            m = chr(o)
        q.appendleft((s + chr(o), m))

Solution (Python):

from queue import deque
n = int(input())
q = deque([("a", "a")])
while True:
    s, m = q.pop()
    if len(s)==n:
        print(s)
    elif len(s)>=n+1:
        break
    for o in range(ord("a"), ord(m)+2):
        if ord(m)<o:
            m = chr(o)
        q.appendleft((s + chr(o), m))

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 10
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))



def main():
    n = ni()

    char = [chr(i) for i in range(97, 97 + 26)]
    d = [0 for _ in range(11)]
    d[0] = 1

    def printer(d):
        s = ""
        for num in d:
            if num != 0:
                s = char[num - 1] + s

        if len(s) == n:
            print(s)
        return

    while d[n] == 0:
        printer(d)

        d[0] += 1

        for i in range(10):
            if d[i] > max(d[i + 1:]) + 1:
                d[i] = 1
                d[i + 1] += 1


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def bfs(N):
    q = deque(["a"])

    res = []
    while q:
        s = q.popleft()
        if len(s) == N:
            res.append(s)
            continue

        n = len(set(list(s)))
        for i in range(n + 1):
            c = chr(ord("a") + i)
            q.append(s + c)

    return res


def main():
    N = int(input())
    ans = bfs(N)
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque


def input():
    return sys.stdin.readline().strip()


MOD = 10 ** 9 + 7
sys.setrecursionlimit(20000000)
INF = float("inf")


def main():
    N = int(input())
    q = deque(["a"])
    cnt = 1
    answer = []
    while cnt <= N:
        d = q.popleft()

        if len(d) == N:
            answer.append(d)
        cnt = max(cnt, len(d))
        b = 0
        for i in range(cnt):
            if ord(d[i]) > b:
                b = ord(d[i])

        for i in range(ord("a"), b + 2):
            q.append(d + chr(i))
    answer.sort()
    print(*answer, sep="\n")


if __name__ == "__main__":
    main()

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

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


s = {}
s["a"] = 0
ans = [s]

for i in range(2, n+1):
    new_s = {}
    for t in s.items():
        for j in range(1, i):
            if t[0][-1] == w[j-1]:
                for k in range(t[1] + 2):
                    new_s[t[0] + w[k]]  = max(t[1], k)

    ans.append(new_s)
    s = new_s

answer = sorted(ans[n-1].keys())

for x in answer:
    
    print(x)

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

n = int(input())
dfs("", ord("a"))

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def dfs(s,mxs):
    if len(s)==n:
        print(s)
    else:
        for i in range(ord('a'),ord(mxs)+1):
            dfs(s+chr(i),chr(i+1) if i ==ord(mxs) else mxs)

def main():
    global n
    n=int(input())
    dfs('','a')
    

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def dfs(s,mxs):
    if len(s)==n:
        print(s)
    else:
        for i in range(ord('a'),ord(mxs)+1):
            dfs(s+chr(i),chr(i+1) if i ==ord(mxs) else mxs)

def main():
    global n
    n=int(input())
    dfs('','a')
    

if __name__=='__main__':
    main()

Solution (Python):
def generator(n):
    if n == 1:
        yield [0]
    else:
        for A in generator(n - 1):
            for i in range(max(A) + 2):
                A.append(i)
                yield A
                A.pop()


n = int(input())
for A in generator(n):
    print(''.join([chr(a + 97) for a in A]))

Solution (Python):

def generator(n):
    if n == 1:
        yield [0]
    else:
        for A in generator(n - 1):
            for i in range(max(A) + 2):
                A.append(i)
                yield A
                A.pop()


n = int(input())
for A in generator(n):
    s = ''
    for a in A:
        s += chr(a + 97)
    print(s)

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

alp = list('abcdefghijklmnopqrstuvwxyz')

ans_li = ['0']
for _ in range(n-1):
    tmp = []
    for a in ans_li:
        last = 0
        for an in a:
            if int(an)>last:
                last = int(an)
        for i in range(last+2):
            tmp.append(a+str(i))
    ans_li = tmp
for bb in ans_li:
    ans = ''
    for b in bb:
        ans += alp[int(b)]
    print(ans)

Solution (Python):
def make_new_vocab(voc):
    max_let = ord(max(voc)) - 97
    let = [chr(ord('a') + i) for i in range(26)]

    for i in range(max_let + 2):
        yield voc + let[i]


n = int(input())
vocabs = ["a"]


for i in range(1, n):
    new_vocabs = []
    for voc in vocabs:
        for p in make_new_vocab(voc):
            new_vocabs.append(p)
    vocabs = new_vocabs

for ans in vocabs:
    print(ans)

Solution (Python):
def dfs(s, mx):
    if len(s) == N:
        print(s)
        return
    for c in range(ord("a"), mx + 1):
        dfs(s + chr(c), mx + 1 if c == mx else mx)

if __name__ == "__main__":
    N = int(input())
    dfs("", ord("a"))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
n = int(input())


def dfs(s):
    if len(s) == n:
        print(s)
        return 0
    for x in map(chr, range(97, ord(max(s))+2)):
        dfs(s+x)


dfs('a')

Solution (Python):
n = int(input())
alp = "abcdefghijklmnopqrstuvwxyz"
ans = []


def dfs(s):
    if len(s) == n:
        ans.append(s)
        return 0
    se = len(set(s))
    for i in range(se+1):
        dfs(s+alp[i])


dfs('a')
print(*ans, sep='\n')

Solution (Python):
n=int(input())
from collections import deque
d=deque(['a'])
c=[chr(i) for i in range(97, 97+26)]
ans=set([])
while d:
  y=d.pop()
  if len(y)==n:
    ans.add(y)
  now=c.index(sorted(y)[-1])
  for z in range(min(now+2,n+1)):
    if z>-1 and len(y)<n:
      d.append(y+c[z])
for z in sorted(list(ans)):
  print(z)

Solution (Python):
import collections
import numpy as np
import sys
import copy
from functools import lru_cache


sys.setrecursionlimit(10**8)

N=int(input())


num2alpha = lambda c: chr(c+96)


def dfs(K,b,s):
    if len(s)==N:
        return b.append(s)
        s=""
    elif len(s)>N:
        return
    elif len(s)<N:
        x = copy.deepcopy(s)
        for i in range(N):
            if i+1-len(set(x))< 2:
                s=x+num2alpha(i+1)
                dfs(N-1, b, s)
B=list()
m=""
dfs(N-1,B,m)
for i in B:
    print(i)

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()

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

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

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

buf = [0]*n

def solv(idx,char):
    aida = n - idx
    if idx == n:
        print("".join(buf))
        return

    for i in range(char + 1):
        buf[idx] = chr(ord('a') + i)
        solv(idx+1,max(i + 1,char))

solv(0,0)

Solution (Python):
from statistics import median


from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect










import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))

def main():
    n = int(input())
    
    def dfs(cnt, s):
        if cnt == n:
            print(s)
            return
        biggest = 'a'
        c = 0
        while c < len(s):
            if s[c] == biggest:
                biggest = chr(ord(biggest)+1)
                c += 1
            else:
                c += 1
        for i in range(0,ord(biggest) - ord('a') + 1):
            sc = chr(ord('a')+i)
            s += sc
            dfs(cnt + 1, s)
            s = s[:-1]
    dfs(0,"")
if __name__ == '__main__':
  main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


n=I()
lis=[[]for i in range(n)]
lis[0].append([1])
for i in range(n-1):
    for j in lis[i]:
        for m in range(max(j)+1):
            lis[i+1].append(j+[m+1])
        

alpha=["a","b","c","d","e","f","g","h","i","j"]
for u in lis[n-1]:
    ans=""
    for j in u:
        ans+=alpha[j-1]
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


n=I()
lis=[[]for i in range(n)]
lis[0].append([1])
for i in range(n-1):
    for j in lis[i]:
        for m in range(max(j)+1):
            lis[i+1].append(j+[m+1])
        

alpha=["a","b","c","d","e","f","g","h","i","j"]
for u in lis[n-1]:
    ans=""
    for j in u:
        ans+=alpha[j-1]
    print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(100000000)
def dfs(s,n,m,subs):
  if len(subs)==n:
    print(subs)
    return
  
  k = len(set(list(subs)))+1

  for i in range(k):
    t = "".join((subs,s[i]))
    dfs(s,n,k,t)

s = "abcdefghij"

n = int(input())
s = list(s[:n])
dfs(s,n,1,"a")