This code finally accept all case in leetcode for this question:=> public boolean wordPattern(String pattern, String s) { HashMap hm = new HashMap(); Set set = new HashSet(); String arr[] = s.split(" "); if(pattern.length() != arr.length){ return false; } for(int i=0; i
Hello Amrita, I am getting only "false" as output trying the same code. package org.leeetcode.problems; import java.util.HashMap; import java.util.Map; public class WordPattern { public static void main(String[] args) {
Hi Sorry my mistake.. The input String is " cat dog dog cat" If you give this input ,it will work...( since we are splitting the string with whitespaces) Or else you need to change the regex, if you want the same input string String[] arr = s.split( ", ") Thanks for pointing out ! Will edit and upload the video again ! Please try and let me know if that works!
hello hi i did it using the same method which u taught in isomorphic strings but it is giving wrong answer would you pls check class Solution { public boolean wordPattern(String pattern, String s) { if(pattern.length()!=s.length()) return false; HashMapl=new HashMap();
You should check the pattern length with String array length instead of just original String length you should replace this : f(pattern.length()!=s.length()) return false; with this : if(pattern.length()!=a.length) return false;
It's logic is almost same that's you already discussed in Isomorphic String problem. Again Thanks a lot.
Yeah..If you learn the logics ...you can solve multiple problems with one logic..
Thanks for this beautiful explanation.
Thank you so much mam
Great explanation 👍👍👍
Sometimes you have to zoom out to see the whole code, or put it in github it's even better. Thanks anyway
Sure I will start putting codes to github
I was about to point it out cuz the regex is given according to the spaces but then saw this comment.
This code finally accept all case in leetcode for this question:=>
public boolean wordPattern(String pattern, String s) {
HashMap hm = new HashMap();
Set set = new HashSet();
String arr[] = s.split(" ");
if(pattern.length() != arr.length){
return false;
}
for(int i=0; i
Hello Amrita,
I am getting only "false" as output trying the same code.
package org.leeetcode.problems;
import java.util.HashMap;
import java.util.Map;
public class WordPattern {
public static void main(String[] args) {
String pattern="abba";
String str="cat, dog, dog, cat";
System.out.println(isWordPattern(pattern,str));
}
private static boolean isWordPattern(String pattern, String s) {
String []str= s.split(" ");
Map hm = new HashMap();
if(pattern.length()!= str.length) {
return false;
}
for(int i=0;i
Hi
Sorry my mistake..
The input String is " cat dog dog cat"
If you give this input ,it will work...( since we are splitting the string with whitespaces)
Or else you need to change the regex, if you want the same input string
String[] arr = s.split( ", ")
Thanks for pointing out ! Will edit and upload the video again !
Please try and let me know if that works!
@@TechnosageLearning it works thanks!!!
Added the disclaimer in the video..Thanks again for pointing it out
how can i become a good software engineer to writing a code
Its not working for "abba" And "dog dog dog dog"
Yes this test case failed
class Solution {
public boolean wordPattern(String pattern, String s) {
Map map=new HashMap();
String[] arr=s.split(" ");
if(pattern.length()!=arr.length) {
return false;
}
for(int i=0; i
i am getting error in this code
Take the code from git repo i shared and then give it a try
hello hi i did it using the same method which u taught in isomorphic strings
but it is giving wrong answer would you pls check
class Solution {
public boolean wordPattern(String pattern, String s) {
if(pattern.length()!=s.length())
return false;
HashMapl=new HashMap();
s=s.trim();
String a[]=s.split(" ");
for(int i=0;i
You should check the pattern length with String array length instead of just original String length
you should replace this :
f(pattern.length()!=s.length())
return false;
with this :
if(pattern.length()!=a.length)
return false;