| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.CountPrefixAndSuffixPairs1; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class CountPrefixAndSuffixPairs1StringComparison : ICountPrefixAndSuffixPairs1 |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n^2 * m), where m is the maximum word length |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="words"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int CountPrefixSuffixPairs(string[] words) |
| 3 | 24 | | { |
| 3 | 25 | | var count = 0; |
| | 26 | |
|
| 26 | 27 | | for (var i = 0; i < words.Length; i++) |
| 10 | 28 | | { |
| 46 | 29 | | for (var j = i + 1; j < words.Length; j++) |
| 13 | 30 | | { |
| 13 | 31 | | if (IsPrefixAndSuffix(words[i], words[j])) |
| 6 | 32 | | { |
| 6 | 33 | | count++; |
| 6 | 34 | | } |
| 13 | 35 | | } |
| 10 | 36 | | } |
| | 37 | |
|
| 3 | 38 | | return count; |
| 3 | 39 | | } |
| | 40 | |
|
| | 41 | | private static bool IsPrefixAndSuffix(string prefixSuffix, string word) |
| 13 | 42 | | { |
| 13 | 43 | | return word.StartsWith(prefixSuffix) && word.EndsWith(prefixSuffix); |
| 13 | 44 | | } |
| | 45 | | } |