Hope to see your solution about Count Prefix and Suffix Pairs II , have a good day!
@techdose4u22 сағат бұрын
yep
@sailendrachettri852114 сағат бұрын
Thank you sir :)
@techdose4u11 сағат бұрын
welcome :)
@gnanaprakashm84312 сағат бұрын
class Solution { public: bool isPrefixAndSuffix(string& pattern, string& str) { if(pattern == str) return true; if(str.substr(0, pattern.length()) == pattern && str.substr(str.length() - pattern.length()) == pattern){ return true; } return false; } int countPrefixSuffixPairs(vector& words) { int res = 0; for(int i = 0; i < words.size(); ++i) { for(int j = i + 1; j < words.size(); ++j) { res += isPrefixAndSuffix(words[i], words[j]); } } return res; } };
@sujanm988717 сағат бұрын
class Solution { public int countPrefixSuffixPairs(String[] words) { int cnt=0; for(int i=0;i
@techdose4u15 сағат бұрын
thanks
@k.g.sasivarthan208210 сағат бұрын
if(words[i].length()>words[j].length()) continue; my code is running without this line and explain me why this line
@tummalapallikarthikeya241222 сағат бұрын
but it is brute force solution right? can we optimize this solution
@techdose4u22 сағат бұрын
yes we can but its difficult. So waiting for part-2 of the question to explain :)
@nitheeshp.s253213 сағат бұрын
why do we move from n-mth index
@techdose4u11 сағат бұрын
because suffix should move from left to right and should end by last char and its length is exactly the same as prefix length
@23reebapatel7922 сағат бұрын
This solution is not being accepted (java) and it shows this error: Compile Error Line 7: error: incompatible types: String[] cannot be converted to List [in __Driver__.java] int ret = new Solution().countPrefixSuffixPairs(param_1); ... the alternative soln I used: class Solution { private boolean isPrefix(String s1, String s2) { return s2.startsWith(s1); } private boolean isSuffix(String s1, String s2) { return s2.endsWith(s1); } public int countPrefixSuffixPairs(String[] words) { int count = 0; for (int i = 0; i < words.length - 1; i++) { for (int j = i + 1; j < words.length; j++) { if (isPrefix(words[i], words[j]) && isSuffix(words[i], words[j])) { count++; } } } return count; } }
@deepvakharia31198 сағат бұрын
It’s because in the driver fn it’s list of Strings and in your code its array of strings