Algo
Algo
1
5. Tower of Hanoi
Write a recursive function to solve the Tower of Hanoi problem. You have n
disks and three rods. The goal is to move all the disks from the first rod to the
third rod, following these rules:
• Only one disk can be moved at a time.
• A disk can only be placed on top of a larger disk or an empty rod.
Example:
n=3 (Number of disks)
Output:
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
8. Permutations of a String
Write a program that generates all the permutations of a string without using
extra data structures (like a list). You should do it recursively.
Example:
str = ”ABC”
Output:
”ABC”, ”ACB”, ”BAC”, ”BCA”, ”CAB”, ”CBA”
2
9. Find the First Non-Repeating Character
Given a string, find the first non-repeating character and return its index. If no
such character exists, return −1.
Example:
str = ”geeksforgeeks”
Output: 2 (The first non-repeating character is ’f’)