Repeated String HackerRank Solution [Optimal Approach]

  Рет қаралды 197,800

JAVAAID - Coding Interview Preparation

JAVAAID - Coding Interview Preparation

Күн бұрын

Пікірлер: 97
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
If you find this tutorial helpful, please do not forget to like , comment, share and It would be great if you can leave your feedback about the tutorial, as I have put a lot of hard work to make things easy for you. Thanks ..!! 🙏🙏🙏
@eyobghebremicael5791
@eyobghebremicael5791 3 жыл бұрын
As the repeated string isn't important, we can calculate the count of a's easily as follows public static int counta(String a, int n){ int len = a.length(); int count=0; //empty string if (len==0){ return 0; } for (int j=0;j
@jenish9854
@jenish9854 3 жыл бұрын
Osm bro...
@randiaz95
@randiaz95 3 жыл бұрын
This implementation is slow for anything above a million
@B3Band
@B3Band 2 жыл бұрын
@@randiaz95 Check mine. Way faster since you don't actually check n letters of the string. Plus this one is lossy. If n > 2^31 the for loop can't handle it because you used an int. It shouldn't even compile.
@raba650
@raba650 4 жыл бұрын
I use JavaScript as my choice for these challenges, but your explanation still allowed me to better understand the problem as we as come up with & understand a solution. Thank for the clearness.
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
You're very welcome!
@navinchainani4721
@navinchainani4721 4 жыл бұрын
Hi send this code in javascript email is chainani.navin97@gmail.com
@pritamhalder5723
@pritamhalder5723 6 ай бұрын
bhai please help me my email and Facebook hacking they are force fully asking money for me very urgent my account please suggest me please 🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@davidtemidayo8541
@davidtemidayo8541 10 ай бұрын
Hello, thank you for this tutorial, i was able to write this using javaScript and it works function evaluateValue(s,n){ const sLength = s.length; const quotient = Math.floor(n / sLength); const r = n % sLength; const freqA = s.match(/a/g).length; const useRemainder= (r > 0) ? s.slice(0, r).match(/a/).length : 0 const a = quotient * freqA + useRemainder; return a; } evaluateValue("abc", 1000000000000);
@TheManishYadav01
@TheManishYadav01 3 жыл бұрын
This is best plateform of learning code, 👍👍
@gamer-zy1uj
@gamer-zy1uj 3 жыл бұрын
Perfect👍 thanks 😊 you taught so calmly and your teaching way is literally nice .keep going on and please upload more videos on the questions which are asked by fang frequently in java itself🙏🙏🙏🙏🙏🙏 please sir🤗
@NitishKumar-un9ev
@NitishKumar-un9ev 2 жыл бұрын
The most toughest thing in HackerEarth is understanding the problem/Question
@pritamhalder5723
@pritamhalder5723 6 ай бұрын
Help me please
@anil2009
@anil2009 4 жыл бұрын
Guruji you are great!!
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
😀
@lasithadulshan7357
@lasithadulshan7357 4 жыл бұрын
Exellent teaching!! Grow up 💪🏽
@jlecampana
@jlecampana 4 жыл бұрын
Fantastic way of solving it! Thank you.
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Happy to help!
@jn2869
@jn2869 8 ай бұрын
Great teaching, you are awesome, keep up the good work
@rocker31590
@rocker31590 3 жыл бұрын
Wow awesome 👏🏼
@darrellwalker2149
@darrellwalker2149 3 жыл бұрын
public static int AddString(string _str, int n) { string newStr = string.Empty; while(newStr.Length < n) { newStr += _str; } return newStr.Substring(0,n).ToCharArray().Where(x=>x.ToString() =="a").Count(); }
@ronaldomaia
@ronaldomaia 7 ай бұрын
const repeatedString = (s, n) => { const countA = s => s.split('').filter(char => char === 'a').length; return countA(s) * Math.floor(n / s.length) + countA(s.slice(0, n % s.length)); }
@priyasurgical4599
@priyasurgical4599 3 жыл бұрын
You are doing Great sir 😎
@AkshaySangolli
@AkshaySangolli 7 ай бұрын
Excellent, but what if the a character is present in the middle of the string i.e. s = "bac"?
@mayanksrivastava4452
@mayanksrivastava4452 4 жыл бұрын
Great as Usual....
@programminginc8539
@programminginc8539 3 жыл бұрын
excellent approach
@archana09424
@archana09424 2 жыл бұрын
Wonderful explanation sir.Thank you for educating us.
@harshmanola563
@harshmanola563 4 жыл бұрын
long size = s.length(), repeated = n/size; long left = n - (size * repeated); int extra = 0; int count = 0; for(int i = 0; i < size; i++){ if(s.charAt(i) == 'a'){ ++count; } } for(int i = 0; i < left; i++){ if(s.charAt(i) == 'a'){ ++extra; } } repeated = (repeated * count) + extra; return repeated;
@B3Band
@B3Band 2 жыл бұрын
Lossy. Your for loop uses int, but it's expected to loop a long number of times.
@hoddybhaba6704
@hoddybhaba6704 Жыл бұрын
Nice explaination!
@JavaAidTutorials
@JavaAidTutorials Жыл бұрын
Glad it was helpful!
@faizandarwesh7867
@faizandarwesh7867 2 жыл бұрын
Quality content.
@youshinde367
@youshinde367 4 жыл бұрын
Great As usual thank you so much
@nareshle3159
@nareshle3159 2 жыл бұрын
Hii sir am Naresh Can you please tell elobourately about coding in eclipse ide so that coding beginners also easily understand
@daromarra5823
@daromarra5823 3 жыл бұрын
Perfect! Thanks!
@Shohan01
@Shohan01 5 ай бұрын
What if the s = ‘bac’ how does that match formula works?
@mohankadam4983
@mohankadam4983 4 жыл бұрын
Thank you so much Sir....It's very helpful
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Most welcome
@michaeljackson6043
@michaeljackson6043 4 жыл бұрын
I will calculate the number of a's of given string and then using n integer , i calculate the n/len(s) and n%len(s) result=(n/len(s)*no.of.a's) +how many chracters in n%len(s) substring
@B3Band
@B3Band 2 жыл бұрын
Similar to my posted solution (i posted the actual java). Substring can only take int (and n is long), but Since String can only hold 2^31, you know casting (n % length) to int will work.
@B3Band
@B3Band 2 жыл бұрын
public static long repeatedString(String s, long n) { int length = s.length(); //length of s long repeats = n / length; //if n is 10 and length is 3, repeats 3 times long append = n % length; //the extra substring at the end of the infinite string String suffix = s.substring(0, (int)append); //10 % 3 = 1, so append one letter to String //we know String can only hold 2^31, so //casting to an int is okay! long count = s.chars().filter(ch -> ch == 'a').count(); //this is how you count chars in Java 8+ //count the a chars in the original string long extraBit = suffix.chars().filter(ch -> ch == 'a').count(); //now count the extra chars from the substring long total = count * repeats + extraBit; //the total number of a chars. Return it! return total; }
@justfun4976
@justfun4976 Жыл бұрын
What is the use of sc.close()
@simsonpeak3119
@simsonpeak3119 2 жыл бұрын
Sir, does string should always start with a, what if the s=bca ,n=10 and 10%3 =1 and 10/3=3 and a occurrence will be 4 as per your code. But actual a occurrence is 3.
@nareshkoude2133
@nareshkoude2133 2 жыл бұрын
Correct !! it won't work...
@sulemani2482
@sulemani2482 Жыл бұрын
first of all thanks sir, but I would like to know can we prepare ourself for .net core on macbook, I am facing an issue of connection string with database on mac after learning c# and sql
@memesmacha61
@memesmacha61 4 жыл бұрын
Loved it..!
@manvirchoudhary107
@manvirchoudhary107 3 жыл бұрын
Thank you sir for the teaching. Would you tell us how we can come up with this kind of mathematical approach ? where we can learn ?. I am able to do simple brute force approach .Request from you please dont stop making videos on problem solving. Please do more videos on that,
@mahmmadhusen6794
@mahmmadhusen6794 4 жыл бұрын
Nice explanation
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Thank you ☺️
@SarfrazRazaOfficial
@SarfrazRazaOfficial 3 жыл бұрын
which software you are using for writing on the board...???
@hareeshhari9293
@hareeshhari9293 4 жыл бұрын
Hi thank you so much for the solution its really helpful I was little confused in partiallength calculation could you please explain
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
You still have this doubt for partial length logic or its cleared now ?
@pasumarthiashik1099
@pasumarthiashik1099 3 жыл бұрын
@@JavaAidTutorials can u explain for loop
@srinivaskaribandi1089
@srinivaskaribandi1089 3 жыл бұрын
@@pasumarthiashik1099 in python my way of sloving s=input() n=int(input()) list_a=[] lenth_of_stng=len(s) nums_add1=n//lenth_of_stng nums_add2=n%lenth_of_stng req1=nums_add1*s req2=s[:nums_add2] total=req1+""+req2 first_letter=total[0] count=0 for j in total: if first_letter==j: count+=1 print(count)
@coffeemaker693
@coffeemaker693 3 жыл бұрын
you no need to concatenate the string, better split string into Individual character first. in C# as easy as : stringObj.ToArray(); in java as simple as : String s1="hello"; char[] ch=s1.toCharArray();
@pasumarthiashik1099
@pasumarthiashik1099 3 жыл бұрын
why have u taken acount and can u explain the for loop because string length in for loop is 3 or 10???
@srinivaskaribandi1089
@srinivaskaribandi1089 3 жыл бұрын
My way of sloving problem like this s=input() n=int(input()) list_a=[] lenth_of_stng=len(s) nums_add1=n//lenth_of_stng nums_add2=n%lenth_of_stng req1=nums_add1*s req2=s[:nums_add2] total=req1+""+req2 first_letter=total[0] count=0 for j in total: if first_letter==j: count+=1 print(count)
@MynameisJanRed
@MynameisJanRed 3 жыл бұрын
Thank you
@JavaAidTutorials
@JavaAidTutorials 3 жыл бұрын
You're welcome
@shraddhapawar5674
@shraddhapawar5674 3 жыл бұрын
Can we edit main function given by hackerrank .
@mohamedfarhan5894
@mohamedfarhan5894 2 жыл бұрын
I have tried python3, and my code works well. But it won't work for very large n values (more than 8-digit) .please check and give suggestions . def char_occ(s,n,char): k = s*n return k[0:n].count(char) examples >char_occ('abcac',10,'a') >4 >char_occ('aba',10,'a') >7
@orchidnewbie
@orchidnewbie 3 жыл бұрын
Thank you for the video. What is the Time Complexity on this solution, max s.Length
@yashwanthchigari14
@yashwanthchigari14 2 жыл бұрын
It's going to be linear: O(n)
@coffeemaker693
@coffeemaker693 3 жыл бұрын
C# version , my solution static void Main(string[] args) { string s = "aba"; Console.WriteLine(RepeatString(10, s)); Console.ReadKey(); } private static int RepeatString(int n, string s) { string result = string.Empty; var characters = s.ToArray(); var countA = 0; for (int i = 0, index = 0; i < n; i++, index++) { if (index == characters.Length) index = 0; if (characters[index] == 'a') countA++; result += string.Concat(characters[index]); } Console.WriteLine(result); return countA; }
@prasuguna
@prasuguna 3 жыл бұрын
If it's bca then this formula fails?
@babr5401
@babr5401 3 жыл бұрын
why you usen't stream ?
@anandkatam8150
@anandkatam8150 2 жыл бұрын
Sir please make hacker rank python preparation kit solutions also please sir
@arjishat
@arjishat 3 жыл бұрын
java7 , java8, java15 er modde partokko ta ki??
@NateFreestyle
@NateFreestyle 8 ай бұрын
HackerRank classifies this as easy? I'm not sure I agree with that.
@hariprasad1727
@hariprasad1727 3 жыл бұрын
How We Need To Set Environment In Web IDE For Spring Boot Applications
@genericbinary558
@genericbinary558 Жыл бұрын
what about "baac" and n=13 🙂
@kuldeepsinghrajput2600
@kuldeepsinghrajput2600 3 жыл бұрын
why u used long for storing values?
@NateFreestyle
@NateFreestyle 8 ай бұрын
To take into account very large input values.
@ashutosh3387
@ashutosh3387 4 жыл бұрын
Is there any complete course of dsa in java
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Currently we do not have any courses yet, but lot of subscribers asked this question, so planning to create in future and inform you all. :)
@samiksharamteke2855
@samiksharamteke2855 2 жыл бұрын
long repeatedString(string s, long n) { long freq = n /s.length(); long remains = n % s.length(); int count = 0; for(int i=0;i
@shenth27
@shenth27 4 жыл бұрын
Quite a bit complicated solution. I'd simply loop n number of times, maintains another index for the s, which I resets every time index >=s.length(). Then in the loop just increment the counter whenever you find 'a'. But I guess not an optimal solution if n is huge number though.
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
yes , if n is huge, it will not pass all TCs
@B3Band
@B3Band 2 жыл бұрын
"simply" loop n times, when n can be 10^12 hahahaha Very slow solution. Check out mine, which only checks the original String, not the infinite string.
@IdontWatchIsee
@IdontWatchIsee 4 жыл бұрын
I didnt understand how you translate the algorithm in code :((( what can i do to understand this part better?? Please help
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Do write lots of code. Learn more about your chosen programming language what are the utils , functionality etc it offers and do practice. you will see the difference. practice is the only key which can quickly help you to translate algorithm into code.
@IdontWatchIsee
@IdontWatchIsee 4 жыл бұрын
@@JavaAidTutorials thank you very much for taking the time to answer ❤️
@abhisekpanda3701
@abhisekpanda3701 2 жыл бұрын
import java.util.Scanner; public class recur { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the word"); String word=sc.next(); System.out.println("Enter the number"); int num=sc.nextInt(); long l=(num/(word.length())); long l1=num%(word.length()); String s=sort(word,l,l1); System.out.println(s); } static String sort(String word,long l , long l1) { String str=""; char ch1=' '; for(int i=0;i
@hariprasad1727
@hariprasad1727 3 жыл бұрын
WEB IDE I am Not able To Understand
@salilkumarghosh9861
@salilkumarghosh9861 4 жыл бұрын
Sir can I solve these problems using C after watching all videos?
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
We always try to teach generic algorithm which is independent of any specific language. so you can use any language to implement the same .
@tauqeerahmed1366
@tauqeerahmed1366 Жыл бұрын
If someone wants to see this in swift let c = "baa" let n = 10 func checkNoOfCharactersIn(souceString : String , character : Character) -> Int { var count = 0 for char in souceString { if character == char { count += 1 } } return count } func getSubstringToString(sourceString : String,to : Int) -> String { return String(sourceString[.. Int { var answer = 0 let lengthOfOneTimeString = sourceString.count let repeatTimese = n / lengthOfOneTimeString let reminderLimit = n % lengthOfOneTimeString answer = checkNoOfCharactersIn(souceString: sourceString, character: "a") answer = answer * repeatTimese answer = answer + checkNoOfCharactersIn(souceString: getSubstringToString(sourceString: sourceString, to: reminderLimit), character: "a") return answer } var count = numberOfTimeAInNLetters(sourceString: c, n: n)
@PrivacyRocks
@PrivacyRocks Ай бұрын
Your heavy India/pakistani accent makes it impossible to understand you. I’m looking for someone else
Utopian Tree HackerRank Solution | Hackerrank Utopian Tree | [One Liner Solution]
23:39
JAVAAID - Coding Interview Preparation
Рет қаралды 35 М.
Sliding Window Technique - Algorithmic Mental Models
36:45
Ryan Schachte
Рет қаралды 374 М.
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
Count Triplets Hackerrank Solution | Interview Preparation Kit
29:59
JAVAAID - Coding Interview Preparation
Рет қаралды 37 М.
Coding Interview | Software Engineer @ Bloomberg (Part 1)
30:05
Keep On Coding
Рет қаралды 4,9 МЛН
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 359 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 327 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 200 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 973 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 518 М.
Longest Common Subsequence(LCS) Dynamic Programming In O(N) Space | EP7
34:19
JAVAAID - Coding Interview Preparation
Рет қаралды 24 М.
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН