some wondering O(n) kha se hai bhai ye? its not O(n^2) because :- The inner loop, despite being inside the for loop, checks only the characters in the map. Since the map can hold at most n elements (one for each character in the string), the overall complexity remains linear, i.e., O(n).
As per leetcode looks like length of string can go till 5 * 10^4. So containsValue() can potentially end up doing nested looping with O(n) time complexity. Maybe we can improve this by having another reverse HashMap to improve the look up time. SpaceWise it will be 2X but I guess space is much cheaper than time :)
Hey nikhil i have a small doubt what happenns if we dont create a new variable mapped char but instead directly compare original with replacement i did that and out of 47 ..12 test cases failed
Java have a method to check the containsValue in the map, but cpp don't have that what should we do like creating another set for storing the values and then like check if the value contains in that something like this? This solved it but what about space complexity using map and set both.
don't go by these numbers you see on leetcode. If your solution is accepted, it is usually a good enough solution. The runtime usually depends on a lot of things: - the startup time of the VM - the language choice - the processor of the VM - the version of software stack
You said: why this shows this error please help.. class Solution { public boolean isIsomorphic(String s, String t) { int n = s.length(); int m = t.length(); if(n!=m) { return false; } Map sam = new HashMap(); for(int i=0;i
@@Electrocode_gk if "paper" is your start string, you are mapping E -> L, and R -> E but if "title" is your start string, then you map L -> E and E -> R You are mapping different characters. But in case 4, when you start from "kikp" you are mapping the same character K once to B and then to D. This is not allowed.
I'm not able to understand the second else condition - else{ char mappedCharacter = charMappingMap.get(original); if (mappedCharacter != replacement)return false;}
Nikhil, you are one of the only people that make these problems simple and easy to understand. I appreciate your efforts very much!
exactly
If you are coding in c++, you will need 2 hashmaps as you can't make sure the letter is not present in the key as well as the value.
Really like the way of your explanation Nikhil , it encourages to solve DS problems which otherwise feels very frustrating.
Your explanations are great and you make them so much easier to understand. Can you also solve '605. Can Place Flowers'?
thank you so much...sure I will add it to my pipeline of upcoming videos.
best teacher, its hard to follow others even if they have same solution as you!
Thankyou!!
You are inspiration sir, keep similing
Thank you so much for making these problems simple and easy to understand, your explanation is best.
u got a new subscriber ,
Thanks Nikhil for amazing quality content
Found best teacher 🏆🏆
problem made extremely intuitive, gg!
Awesome explanation sir
some wondering O(n) kha se hai bhai ye?
its not O(n^2) because :-
The inner loop, despite being inside the for loop, checks only the characters in the map. Since the map can hold at most n elements (one for each character in the string), the overall complexity remains linear, i.e., O(n).
Bro you should learn java from the start the method he used contains value run in O(n) that's why this O(n2)
As per leetcode looks like length of string can go till 5 * 10^4. So containsValue() can potentially end up doing nested looping with O(n) time complexity. Maybe we can improve this by having another reverse HashMap to improve the look up time. SpaceWise it will be 2X but I guess space is much cheaper than time :)
Even if the string length is huge, you only have 256 different characters. This is almost constant time.
U are great , no idea why u are undeerrated
Really Appreciate your efforts
Great explaination!
u dont need if condition at start in constraints its given that two strings are of same length
there can be a chance of different constraints. So that case is mandatory
Very nice explanation !! Thank you!!
Glad it was helpful!
Amazing sirr!! We love you..........
nice explanation
Your code timecomplexity is O(n^2) and timecomplexity O(n)
great explanation
Wow.. simply good sir
Hey nikhil i have a small doubt
what happenns if we dont create a new variable mapped char but instead directly compare original with replacement
i did that and out of 47 ..12 test cases failed
why don't we do like this:
finding charecter frequency arrays for both strings.
sorting them ...and comparing the sorted values.
is this code written in java or C++
thanx brother
Java have a method to check the containsValue in the map, but cpp don't have that what should we do like creating another set for storing the values and then like check if the value contains in that something like this? This solved it but what about space complexity using map and set both.
If you use std::unordered_map, which is implemented as a hash table, the average case time complexity for insertion, deletion, and search is O(1).
thanks, bhaiya got it@@nikoo28
cool stuff.
bhaiya this code passed 35/44 test cases
class Solution {
public boolean isIsomorphic(String s, String t) {
ArrayList s1 = new ArrayList();
ArrayList s2 = new ArrayList();
int count = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1)) {
count++;
} else {
s1.add(count);
count = 1;
}
}
// s1.add(count);
int count1 = 1;
for (int i = 1; i < t.length(); i++) {
if (t.charAt(i) == t.charAt(i - 1)) {
count1++;
} else {
s2.add(count1);
count1 = 1;
}
}
s2.add(count1);
return s1.equals(s2);
}
}
have a look at the code in the video description. You will find a github link. That code passes all cases :)
@@nikoo28 thank you bhaiya
Understood
This solution has runtime of 10ms and beats only 68%. Do you have a faster solution?
don't go by these numbers you see on leetcode. If your solution is accepted, it is usually a good enough solution. The runtime usually depends on a lot of things:
- the startup time of the VM
- the language choice
- the processor of the VM
- the version of software stack
Thanks
easy way to solve this is l=len(set(zip(s,t)) a=len(s) b=len(t) return l==a==b: how easy it is using length
You said:
why this shows this error please help.. class Solution {
public boolean isIsomorphic(String s, String t) {
int n = s.length();
int m = t.length();
if(n!=m)
{
return false;
}
Map sam = new HashMap();
for(int i=0;i
Are "afa" and "dde" Isomorphic or not?
they are not
u so sick dudeee
Will this code pass the test case
s = "12" t = "\u0067\u0068"
did you try it out?
❤
😍😍😍😍😍😍😍😍😍😍😍
how Paper and Title is isomorphic string you are teching wrong to students
public boolean isIsomorphic(String s, String t) {
int n1 = s.length();
int n2 = t.length();
if (n1 != n2) {
return false;
}
HashMap mapST = new HashMap();
HashMap mapTS = new HashMap();
for (int i = 0; i < n1; i++) {
char c1 = s.charAt(i);
char c2 = t.charAt(i);
if (mapTS.containsKey(c1) && mapTS.get(c1) != c2) {
return false;
}
if (mapST.containsKey(c2) && mapST.get(c2) != c1) {
return false;
}
mapTS.put(c1, c2);
mapST.put(c2, c1);
}
return true;
}
very bad video
can you please let me know which part was difficult to understand?
@@nikoo28 first description part it's self u confused
which timestamp is confusing you? I added 4 different test cases to clear any confusion.
@@nikoo28 paper title how e r l e can be mapped e is common 4 case also true
@@Electrocode_gk if "paper" is your start string, you are mapping E -> L, and R -> E
but if "title" is your start string, then you map L -> E and E -> R
You are mapping different characters.
But in case 4, when you start from "kikp" you are mapping the same character K once to B and then to D. This is not allowed.
won't the time complexity be O(N^2) as the the containsValue() method goes through all the key value pairs in a hashmap?
can someone please clarify
yes same doubt
@nikoo28 can you please reply
I'm not able to understand the second else condition - else{ char mappedCharacter = charMappingMap.get(original); if (mappedCharacter != replacement)return false;}
Just think opposite like s1 = "kikp" and s2 = "badc" . I hope you got it
def isIsomorphic(self, s, t):
map1 = []
map2 = []
for idx in s:
map1.append(s.index(idx))
for idx in t:
map2.append(t.index(idx))
if map1 == map2:
return True
return False