cs: add «3042. Count Prefix and Suffix Pairs I»
URL: https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
6dfd741e6a
commit
083d7c3da1
1 changed files with 18 additions and 0 deletions
18
cs/count-prefix-and-suffix-pairs-i.cs
Normal file
18
cs/count-prefix-and-suffix-pairs-i.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
public class Solution {
|
||||
private bool IsPrefixAndSuffix(string str1, string str2) =>
|
||||
str1.Length <= str2.Length && str2.StartsWith(str1) && str2.EndsWith(str1);
|
||||
|
||||
public int CountPrefixSuffixPairs(string[] words) {
|
||||
var count = 0;
|
||||
|
||||
for (var i = 0; i < words.Length; ++i) {
|
||||
for (var j = i + 1; j < words.Length; ++j) {
|
||||
if (IsPrefixAndSuffix(words[i], words[j])) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue