map.containsValue has O(N) time complexity if instead of 1 Map you use 2 Maps you can improve the time complexity while sacrificing the space
@wolfofthelight56904 жыл бұрын
One thing I had an issue with here was that the pattern length must equal the string length. I passed the leetcode problem before coming here, but it was the last issue I went into. The question is ambiguous. If it is a pattern, there is no reason the string cannot be longer. For example "abba" and "dog cat cat dog dog cat cat dog". In my original solution to the problem, I accounted for this.... the sentence still "fits" the pattern. The problem does not accept this, however, and I removed it and passed the leetcode problem easily. The reason I comment is, if you are asked this in an interview or something, be prepared to ask whether the pattern can recur or if its length is an actual integral part (in which case, it really isn't a pattern so much as simply a one-to-one mapping).
@pushpendrasingh777 Жыл бұрын
Tried this same algorithm using the c++ was not able to get it but now it seems easy...
@samandarboymurodov89413 жыл бұрын
Thank you. it is clean and easy to understand
@Ben-pb7ct3 жыл бұрын
always one of the best explanation
@bofeng78744 жыл бұрын
Thank you! Easy to understand!
@sandipchanda65224 жыл бұрын
Nice solution
@cupofjava54802 жыл бұрын
better way is using two HashMaps
@1314rom3 жыл бұрын
thank u ur awesome!
@0anant04 жыл бұрын
Elegant!
@ramsharma64252 жыл бұрын
doest looks a "easy " problem to me
@sriramsatvikatukuri33414 жыл бұрын
class Solution { public boolean wordPattern(String pattern, String str) { String []sa=str.split(" "); if(pattern == null || sa.length!=pattern.length()) return false; int n=pattern.length(); Map m=new HashMap(); Map m1=new HashMap(); for(int i=0;i
@aayushpagare93664 жыл бұрын
It's a request please don't use Java ....use c++ in your videos🙏🙏🙏🙏🙏🙏
@melvinkimathi89243 жыл бұрын
He dreams in Java bro
@saranshagrawal34482 жыл бұрын
c++ is noob language and if u have problem with java then go for other source🥴