0% found this document useful (0 votes)
44 views2 pages

Recursion Problem-Solving Techniques

To solve recursive problems, break them into three components: the bigger problem (the overall problem), the smaller problem (a sub-problem that is assumed to work recursively), and the self work (the work done to solve the smaller problem and make it part of the bigger problem). For example, to find the maximum element in an array recursively: the bigger problem is finding the max in the whole array, the smaller problem is assuming we can find the max in the rest of the array, and the self work is comparing the max from the rest with the first element.

Uploaded by

piyush.vermacs22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

Recursion Problem-Solving Techniques

To solve recursive problems, break them into three components: the bigger problem (the overall problem), the smaller problem (a sub-problem that is assumed to work recursively), and the self work (the work done to solve the smaller problem and make it part of the bigger problem). For example, to find the maximum element in an array recursively: the bigger problem is finding the max in the whole array, the smaller problem is assuming we can find the max in the rest of the array, and the self work is comparing the max from the rest with the first element.

Uploaded by

piyush.vermacs22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Hacks

To Solve any Problem related to Recursion All you need to do is Break the Problem
into 3 important components which are as follows :-

Bigger Problem : The original Problem statement is your bigger problem.


Smaller Problem: In every recursive prblm there exist a prblm statement which you
need to achieve in order to fullfill the prblm statement but by considering such a
smaller prblm from the bigger prblm is needed which we need to assume that the
recursion will work and will give the answer.
Self Work: The amount of work which one should do in order to make the smaller
problem your problem.
For e.g.., In order to find the max of any array, three components will be :-
Bigger Problem : To find the max in whole array viz find max in array from index 0
to n - 1.
Smaller Problem: Assume that the recursion works and will find the max of array
from index 1 to n - 1.
Self Work : In order to make your smaller prblm your bigger prblm all you need to
do is to compare the ans return by assuming the smaller prblm works with the 0th
index element and return the max among both of them.

Approach :
Bigger Problem : To Print all of the possible Strings from the given array that
contains code generated by given number.
Smaller Problem : Assume the recursion works and will give you ans for N - 1 length
of String.
Self Work : In order to make your smaller problem all you need to do is work for
the 0th index element. Because it could be the part included in the answer or not.
and self work is to use first character as it is and another time with the next
variable.For e.g..,
In 123 either use 1 as it is or use code for 12 instead.
Note :In the Base all you need to do is to check whether the string code genertated
from the number is contained in any of the string array given in the Question.

Java Code

static String table[] = { " ", ".+@$", "abc", "def", "ghi", "jkl", "mno", "pqrs",
"tuv", "wxyz" };

private static final String[] searchIn = { "prateek", "sneha", "deepak",


"arnav", "shikha", "palak", "utkarsh",
"divyam", "vidhi", "sparsh", "akku" };

public static void keypad1(String ques, String ans) {

if (ques.length() == 0) {
for (String ss : searchIn) {
if (ss.contains(ans)) {
System.out.println(ss);
}
}

return;
}

char ch = ques.charAt(0);
String ros = ques.substring(1);
String code = table[ch - '0'];

for (int i = 0; i < code.length(); i++)


keypad1(ros, ans + code.charAt(i));

C++ Code

#include <bits/stdc++.h>
using namespace std;

vector<string> table = {" ", ".+@$", "abc", "def", "ghi", "jkl", "mno", "pqrs",
"tuv", "wxyz"};

vector<string> searchIn = {
"prateek", "sneha", "deepak", "arnav", "shikha", "palak",
"utkarsh", "divyam", "vidhi", "sparsh", "akku"};

void phoneKeypad(string inp, string out)


{
//Base case
if (inp.size() == 0)
{
for (string ss : searchIn)
{
if (ss.find(out) != ss.npos)
{
cout << ss << endl;
}
}
return;
}

//Rec case
char ch = inp[0];
string ros = inp.substr(1);
string code = table[ch - '0'];

for (int i = 0; i < code.length(); i++)


phoneKeypad(ros, out + code[i]);
}

int main()
{
string input;
cin >> input;

string output;
phoneKeypad(input, output);
return 0;
}

You might also like