Count Prefix and Suffix Pairs I | Leetcode 3042

  Рет қаралды 2,464

Techdose

Techdose

Күн бұрын

Пікірлер: 14
@LinhHoang-ml1qo
@LinhHoang-ml1qo Күн бұрын
Hope to see your solution about Count Prefix and Suffix Pairs II , have a good day!
@techdose4u
@techdose4u 22 сағат бұрын
yep
@sailendrachettri8521
@sailendrachettri8521 14 сағат бұрын
Thank you sir :)
@techdose4u
@techdose4u 11 сағат бұрын
welcome :)
@gnanaprakashm843
@gnanaprakashm843 12 сағат бұрын
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; } };
@sujanm9887
@sujanm9887 17 сағат бұрын
class Solution { public int countPrefixSuffixPairs(String[] words) { int cnt=0; for(int i=0;i
@techdose4u
@techdose4u 15 сағат бұрын
thanks
@k.g.sasivarthan2082
@k.g.sasivarthan2082 10 сағат бұрын
if(words[i].length()>words[j].length()) continue; my code is running without this line and explain me why this line
@tummalapallikarthikeya2412
@tummalapallikarthikeya2412 22 сағат бұрын
but it is brute force solution right? can we optimize this solution
@techdose4u
@techdose4u 22 сағат бұрын
yes we can but its difficult. So waiting for part-2 of the question to explain :)
@nitheeshp.s2532
@nitheeshp.s2532 13 сағат бұрын
why do we move from n-mth index
@techdose4u
@techdose4u 11 сағат бұрын
because suffix should move from left to right and should end by last char and its length is exactly the same as prefix length
@23reebapatel79
@23reebapatel79 22 сағат бұрын
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; } }
@deepvakharia3119
@deepvakharia3119 8 сағат бұрын
It’s because in the driver fn it’s list of Strings and in your code its array of strings
Learn Git - The Full Course
4:20:00
Boot dev
Рет қаралды 128 М.
Search in Rotated Sorted Array | Binary Search | Leetcode 33
19:30
Apna College
Рет қаралды 59 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
Counting Words With a Given Prefix | Leetcode 2185
12:01
Pointers in C++ | In Detail | DSA Series by Shradha Ma'am
46:08
Apna College
Рет қаралды 121 М.
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 152 М.
Retro MS-DOS Coding - Recreating the Iconic Award BIOS Screen
18:16
NCOT Technology
Рет қаралды 66 М.
String Matching in an Array | Leetcode 1408
19:20
Techdose
Рет қаралды 4,3 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 722 М.
Lecture 1: Introduction to CS and Programming Using Python
1:03:30
MIT OpenCourseWare
Рет қаралды 969 М.