Print Encodings - Solution | Recursion | Data Structure and Algorithms in JAVA

  Рет қаралды 37,636

Pepcoding

Pepcoding

Күн бұрын

Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we discuss the solution of the Print Encodings problem where we are required to print all the strings of alphabets according to the input given as numbers where every alphabet is assigned a value of an integer. To understand the problem statement clearly, click here:
For a better experience and more exercises, VISIT: www.pepcoding....
#pepcoding #java #programming
Have a look at our result: www.pepcoding....
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education

Пікірлер: 87
@chopchop17
@chopchop17 3 жыл бұрын
code in c++ with slightly different approach: #include using namespace std; void pencod(string s, string ans) { if (s.length()==0) { cout
@nandinisharma5495
@nandinisharma5495 2 жыл бұрын
thanks buddy for the code
@anjulatakashyap6974
@anjulatakashyap6974 3 жыл бұрын
One of the best teacher i have ever seen.
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad to know that you liked the content and thank you for appreciating. The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos. So, keep motivating, keep learning and keep loving Pepcoding😊
@yoyo8293
@yoyo8293 4 жыл бұрын
Sir apka code thoda complicated hogaya , maine call smart ,base case normal approach lagaya aur accept hogaya : - public static void encodings(String que,String ans,char[] alpha) { if(que.length()==0) { System.out.println(ans); return ; } char ch=que.charAt(0); if(ch=='0') { return ; } char smallAns=alpha[ch-'0']; String ros=que.substring(1); encodings(ros,ans+smallAns,alpha); if(que.length()>=2) { String idx=que.substring(0,2); int index=10*(idx.charAt(0)-'0'); if(index==0) { return ; } index+=(idx.charAt(1)-'0'); if(index
@pankajbisht897
@pankajbisht897 3 жыл бұрын
HI Summet it would be of immense help if you could state the brute force approach. Maybe for some questions brute force could help to get to an optimum sution
@vasudevparmar9876
@vasudevparmar9876 3 жыл бұрын
Just like key pad combinations video, we can define global variable here to It will reduce the code
@prabhsharansingh3805
@prabhsharansingh3805 3 жыл бұрын
bro means if we change a,b,c instead of keypad then we don't have to check for ques.length() for 1 and 2? and we can simply apply the keypad code with required changes ?
@raj_kundalia
@raj_kundalia 2 жыл бұрын
Watched on 22 Nov, 2021 - Thanks for the video.
@Pepcoding
@Pepcoding 2 жыл бұрын
Hope you like the video for better experience and well-organised content visit on nados.pepcoding.com. Also you can post your query on Community tab. Don't forget to follow us on Instagram instagram.com/pepcoding/
@pratyush__agarwal
@pratyush__agarwal 3 жыл бұрын
Very well explained ❤️
@Pepcoding
@Pepcoding 3 жыл бұрын
Thankyou!
@aashishgoyal1436
@aashishgoyal1436 4 жыл бұрын
thanks a lot Sir for explaining the soln in easy to understand manner
@Pepcoding
@Pepcoding 4 жыл бұрын
You are most welcome
@raghavagrawal6263
@raghavagrawal6263 Жыл бұрын
What is the mistake in below code. It is running fine and output is also coming then also its giving wrong answer. Please correct me. public static void printEncodings(String str, String ans) { if(str.equals("")) { System.out.println(ans); return; } char c = str.charAt(0); if(c != '0') { int n = c - '0'; String rem_str = str.substring(1); char ch = (char)(96+n); printEncodings(rem_str, ans+ch); } if(str.length() >= 2) { String s = str.substring(0, 2); int m = Integer.parseInt(s); if(m >= 10 && m
@FirstLearningthanEarning
@FirstLearningthanEarning 4 жыл бұрын
Good morning Sir... I want to join Placement Bootcamp Course... I am 4th year Student.... Huge fan of yours.... Admiring u everytime just can be 1% of you i wish...
@Pepcoding
@Pepcoding 4 жыл бұрын
All the best. Placement Program October mei shuru hoga.
@himanshuchhikara4918
@himanshuchhikara4918 4 жыл бұрын
video is very hepful sir thankyou
@Pepcoding
@Pepcoding 4 жыл бұрын
Most welcome
@mahakjain3732
@mahakjain3732 2 жыл бұрын
Can we do the same question with Recursion with ArrayList?
@mickyman753
@mickyman753 3 жыл бұрын
itne tough question ko kitna easy bna diya sir ne
@Pepcoding
@Pepcoding 3 жыл бұрын
Thank you so much and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@SCRIPTSAG
@SCRIPTSAG 4 жыл бұрын
Thoda bahut soch paya tha sir kuch aisha logic 1 hour diye the sir
@Pepcoding
@Pepcoding 4 жыл бұрын
bdiiaa haii yrr!! keep practicing
@SCRIPTSAG
@SCRIPTSAG 3 жыл бұрын
Esme hmesa one charcter nikalte hai ya fir pahle 2 lekin hme pahle 2 me bhi ho sakta hai koi 27 ho to uske liye bhi usko int me convert kerke check kiya fir use ch me convert kiya aur call kiya hai
@RinkuYadav-pn4jo
@RinkuYadav-pn4jo 2 жыл бұрын
sir here is a simpler (i think) or may be cleaner implementation of the solution (in python i code): encodings = ["0", 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] def printEnc(nu, ans): if len(nu) == 0: print(ans) return if int(nu[0]) == 0: return if len(nu) >= 1: printEnc(nu[1:], ans + encodings[int(nu[0])]) if int(nu[:2]) = 2: printEnc(nu[2:], ans + encodings[int(nu[:2])])
@SubodhKumar-vf4si
@SubodhKumar-vf4si 2 жыл бұрын
is this solution right fot a test case lets say 145 ? here we can take 1,4,5 individuallly but if we take 1 separately and 45 separately it will not work !
@DeepakGupta-zz1vf
@DeepakGupta-zz1vf 3 жыл бұрын
bhaiya yaha agar first char 0 hai toh aage jaane ki jarurat hi nahi na uske dono possibilities cancel hi hojaegi, toh kya hum ek base condition rak de kya ki if(ques.charAt('0') == '0') then return???
@Pepcoding
@Pepcoding 3 жыл бұрын
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
@anjneykumarsingh4461
@anjneykumarsingh4461 4 жыл бұрын
Sir length 1 p bhi call lgni chaiye n Or else length 0 wale ko eliminate kr skte h
@SCRIPTSAG
@SCRIPTSAG 3 жыл бұрын
Yes bro
@ishanraja8162
@ishanraja8162 2 жыл бұрын
Wahi mai bhi nhi samjha length 1 pe call lagegi roq ki tab he toh return hoga. Unhone direct print kar diya
@tushargupta3236
@tushargupta3236 4 жыл бұрын
Blank testcases tab pass ho jaenge jab aap new line print kar denge too if(ques.length == 0){ cout
@aaquibshahzada9761
@aaquibshahzada9761 2 жыл бұрын
sir chv12 should be greater then 10
@sudhindrashenoy7992
@sudhindrashenoy7992 3 жыл бұрын
Hello Sir...Please tell when should we add a return statement in recursion. I am really confused about it.
@Pepcoding
@Pepcoding 3 жыл бұрын
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
@sudhindrashenoy7992
@sudhindrashenoy7992 3 жыл бұрын
@@Pepcoding sure Sir. Thank you.
@mickyman753
@mickyman753 3 жыл бұрын
hamne aage uske call hi nhi lagai koi in that else if block ,toh waise hi function uthar end ho gya ,but yes wha return lagana chahiye
@stellagabriella9085
@stellagabriella9085 3 жыл бұрын
I have searched all your videos, please tell the link where you have explained how to draw the tree required in this video Eagerly waiting
@Pepcoding
@Pepcoding 3 жыл бұрын
It is by name print zig zag
@yashwantkumar9929
@yashwantkumar9929 4 жыл бұрын
Sir, agar a = 1, b = 2, c = 3, Na hoker Special keywords hote to Kaise approach karte Like a = _*_ b = __ c = *__* ... z = **_ Esme condition nhi lagenge n zero ke liye 1 ke ya usse jada ke lie
@simransaigal5896
@simransaigal5896 4 жыл бұрын
Sir I have done the encoding as asf+char(str.charAt(0)-‘1’+’a’) But it is throwing an exception saying wrong expression. Please help.
@komaldeepsingh6356
@komaldeepsingh6356 3 жыл бұрын
you are adding 'char' to 'string' ..... so it is throwing u an error
@arshadmanxoori07
@arshadmanxoori07 3 жыл бұрын
sir if ch12 == 0 ho jaaata hai to kya?? uske liye to likha hi nhi still code fine hai..how?
@monu_7712
@monu_7712 2 жыл бұрын
Same doubt we have to modify that
@octagonalcoder8048
@octagonalcoder8048 3 жыл бұрын
what will be the time complexity??
@Pepcoding
@Pepcoding 3 жыл бұрын
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
@octagonalcoder8048
@octagonalcoder8048 3 жыл бұрын
@@Pepcoding ohky sure
@utkarshagarwal587
@utkarshagarwal587 3 жыл бұрын
O(length of string)
@priyanshgoyal9417
@priyanshgoyal9417 3 жыл бұрын
this is giving time limit reached error for very large input such as "1111111111111111111111" . any idea on how to optimize it.
@saarthakaggarwal7018
@saarthakaggarwal7018 3 жыл бұрын
Use simple 1-D dp
@037_sayedramishali7
@037_sayedramishali7 4 жыл бұрын
Sir c++ mai maine Stoi fxn use kara string to int ke liye but vo kaam nhi karrha sir
@Pepcoding
@Pepcoding 4 жыл бұрын
Stoi ke liee nhi to function bnaa lijieee apnaa
@sommayghosh4617
@sommayghosh4617 3 жыл бұрын
stoi len 2 k liye chal jayega , single length actually character hota h toh stoi kaam ni krega, usse khud se krna pdega str[i]-'0' use krke
@eminemrokzz8182
@eminemrokzz8182 Жыл бұрын
A littile bit shorted version of this code public static void printEncodings(String str, String asf) { if (str.length() == 0) { System.out.println(asf); return; } if(str.charAt(0) == '0') return; // First choice i.e length number for (int i = 0; i < str.length(); i++) { String sub = str.substring(0, i + 1); int no = Integer.parseInt(sub); if (no > 26) { return; } String remstr = str.substring(i + 1, str.length()); printEncodings(remstr, asf + (char)(no - 1 + 'a')); } }
@mihirshinde4268
@mihirshinde4268 Жыл бұрын
public static void main(String[] args) throws Exception { Scanner scn = new Scanner(System.in); String ques = scn.next(); printEncodings(ques, ""); } public static void printEncodings(String ques, String ans) { if (ques.length() == 0) { System.out.println(ans); return; } // 1st option // first 1 digit from 123 [1] char ch = ques.charAt(0); String roq1 = ques.substring(1); if (ch == '0') { return; } else { int chv = ch - '0'; char letter = (char) ('a' + chv - 1); printEncodings(roq1, ans + letter); } // 2nd option - first 2 digits from 123 [12] if (ques.length() >= 2) { String ch2 = ques.substring(0, 2); String roq2 = ques.substring(2); if (ch2.charAt(0) == '0') { return; } else { int chv = Integer.parseInt(ch2); if (chv
@tanmaya2421
@tanmaya2421 Жыл бұрын
public static void printEncodings(String s, String ans) { if(s.startsWith("0")){ return; } if(s.length() == 0){ System.out.println(ans); return; } char ch = (char)(s.charAt(0) +'0'); printEncodings(s.substring(1), ans + ch); if(s.length() > 1 && Integer.parseInt(s.substring(0,2))
@wtf_nikhil
@wtf_nikhil 4 ай бұрын
bhot complicated ho gya sir yeh wala
@Story.Blocks
@Story.Blocks 3 жыл бұрын
@pepcoding Sir online editor me language select ni ho ra..
@Pepcoding
@Pepcoding 3 жыл бұрын
Beta dubara try kr k dekho.
@12345689atul
@12345689atul 3 жыл бұрын
hi sir , for long string its giving TLE , can u please guide me for longer strings also for example with string "111111111111111111111111111111111111111111111" this is not working on local machine
@nishantkumarsingh6421
@nishantkumarsingh6421 3 жыл бұрын
hey we are only using int it is very large
@utkarshagarwal587
@utkarshagarwal587 3 жыл бұрын
use BigInteger in Java or long long int in cpp
@ajitmishra7689
@ajitmishra7689 2 жыл бұрын
just memoize by creating hashmap and add ans to that map and check if it is already there for every recursion call it will reduce the complexity.
@shivampundir1829
@shivampundir1829 3 жыл бұрын
ye do dislikes coding ninjas aur coding blocks walo ke hai...
@Pepcoding
@Pepcoding 3 жыл бұрын
Haha Keep learning. Keep supporting.
@anjneykumarsingh4461
@anjneykumarsingh4461 4 жыл бұрын
Sorry sir smjh gye. copy pen bhut jaruri
@Pepcoding
@Pepcoding 4 жыл бұрын
cool
@SCRIPTSAG
@SCRIPTSAG 4 жыл бұрын
Ha bhai copy pen pe tree bna ke serial wise dry run kerne ke bad samgh ata hai ki memory me kya ho rha hai chel kaise rha hai code ye sab mja ata hai dry run me sochne ke time
@AnjaliSingh-uq9fy
@AnjaliSingh-uq9fy 3 жыл бұрын
@@SCRIPTSAG ek doubt h suppose str = 231 h toh voh case kb chlega jb hum 2(single) aur 31(pair) bnaege? code me toh 1st 2 no ka hi pair bn rha h for eg 23 and 1?
@sakshigupta8141
@sakshigupta8141 3 жыл бұрын
@@AnjaliSingh-uq9fy nhi, code mein starting 2 numbers ka bna rha jaisr ki 2 and 23 ka
@AnjaliSingh-uq9fy
@AnjaliSingh-uq9fy 3 жыл бұрын
@@sakshigupta8141 toh hamesha first 2 no ka hi pair bnega?
@maitohpughu5714
@maitohpughu5714 Жыл бұрын
********JAVASCRIPT CODE********* (OPEN FOR SUGGESTIONS) function printEncodings(num, res) { if (num.length === 0) { console.log(res); return; } else if (num.length === 1) { if (num[0] === "0") { return; } console.log(res + codes[+num[0]]); } else { let firstChar = +num[0]; let restChar = num.slice(1); if (firstChar === 0) { return; } else { printEncodings(restChar, res + codes[firstChar]); } let firstTwo = +num.slice(0, 2); let rest = num.slice(2); if (firstTwo
РОДИТЕЛИ НА ШКОЛЬНОМ ПРАЗДНИКЕ
01:00
SIDELNIKOVVV
Рет қаралды 3 МЛН
How Strong is Tin Foil? 💪
00:26
Preston
Рет қаралды 142 МЛН
Офицер, я всё объясню
01:00
История одного вокалиста
Рет қаралды 5 МЛН
pumpkins #shorts
00:39
Mr DegrEE
Рет қаралды 59 МЛН
РОДИТЕЛИ НА ШКОЛЬНОМ ПРАЗДНИКЕ
01:00
SIDELNIKOVVV
Рет қаралды 3 МЛН