-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution to "Count Prefix and Suffix Pairs I" problem
- Loading branch information
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/com/github/dkoval/leetcode/challenge/CountPrefixAndSuffixPairs1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.github.dkoval.leetcode.challenge; | ||
|
||
/** | ||
* <a href="https://leetcode.com/problems/count-prefix-and-suffix-pairs-i">Count Prefix and Suffix Pairs I</a> | ||
* <p> | ||
* You are given a 0-indexed string array words. | ||
* <p> | ||
* Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2: | ||
* <p> | ||
* isPrefixAndSuffix(str1, str2) returns true if str1 is both a prefix and a suffix of str2, and false otherwise. | ||
* <p> | ||
* For example, isPrefixAndSuffix("aba", "ababa") is true because "aba" is a prefix of "ababa" and also a suffix, but isPrefixAndSuffix("abc", "abcd") is false. | ||
* <p> | ||
* Return an integer denoting the number of index pairs (i, j) such that i < j, and isPrefixAndSuffix(words[i], words[j]) is true. | ||
* <p> | ||
* Constraints: | ||
* <ul> | ||
* <li>>1 <= words.length <= 50</li> | ||
* <li>>1 <= words[i].length <= 10</li> | ||
* <li>>words[i] consists only of lowercase English letters.</li> | ||
* </ul> | ||
*/ | ||
public interface CountPrefixAndSuffixPairs1 { | ||
|
||
int countPrefixSuffixPairs(String[] words); | ||
|
||
class CountPrefixAndSuffixPairs1Rev1 implements CountPrefixAndSuffixPairs1 { | ||
|
||
@Override | ||
public int countPrefixSuffixPairs(String[] words) { | ||
final var n = words.length; | ||
|
||
var count = 0; | ||
for (var i = 0; i < n - 1; i++) { | ||
for (var j = i + 1; j < n; j++) { | ||
if (isPrefixAndSuffix(words[i], words[j])) { | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
private boolean isPrefixAndSuffix(String s1, String s2) { | ||
final var w1 = s1.length(); | ||
final var w2 = s2.length(); | ||
|
||
if (w1 > w2) { | ||
return false; | ||
} | ||
|
||
for (var left = 0; left < w1; left++) { | ||
if (s1.charAt(left) != s2.charAt(left)) { | ||
return false; | ||
} | ||
|
||
if (s1.charAt(w1 - left - 1) != s2.charAt(w2 - left - 1)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/test/kotlin/com/github/dkoval/leetcode/challenge/CountPrefixAndSuffixPairs1Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.github.dkoval.leetcode.challenge | ||
|
||
import com.github.dkoval.leetcode.challenge.CountPrefixAndSuffixPairs1.CountPrefixAndSuffixPairs1Rev1 | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.ArgumentsProvider | ||
import org.junit.jupiter.params.provider.ArgumentsSource | ||
import java.util.stream.Stream | ||
|
||
internal class CountPrefixAndSuffixPairs1Test { | ||
|
||
class InputArgumentsProvider : ArgumentsProvider { | ||
|
||
override fun provideArguments(context: ExtensionContext): Stream<out Arguments> = Stream.of( | ||
Arguments.of( | ||
arrayOf("a", "aba", "ababa", "aa"), | ||
4 | ||
), | ||
Arguments.of( | ||
arrayOf("pa", "papa", "ma", "mama"), | ||
2 | ||
), | ||
Arguments.of( | ||
arrayOf("abab", "ab"), | ||
0 | ||
) | ||
) | ||
} | ||
|
||
@Nested | ||
inner class CountPrefixAndSuffixPairs1Rev1Test { | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(InputArgumentsProvider::class) | ||
fun `should return the number of pairs and isPrefixAndSuffix(words(i), words(j)) is true`( | ||
words: Array<String>, | ||
expected: Int | ||
) { | ||
CountPrefixAndSuffixPairs1Rev1().test(words, expected) | ||
} | ||
} | ||
} | ||
|
||
private fun CountPrefixAndSuffixPairs1.test(words: Array<String>, expected: Int) { | ||
val actual = countPrefixSuffixPairs(words) | ||
assertEquals(expected, actual) | ||
} |