File tree 2 files changed +51
-0
lines changed
2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ package other
2
+
3
+ import java.text.Normalizer
4
+ import java.util.regex.Pattern
5
+
6
+
7
+ fun isPalindrome (text : String ): Boolean {
8
+
9
+ val normalizedText = text.normalize()
10
+ for (i in normalizedText.indices)
11
+ if (normalizedText[i] != normalizedText[normalizedText.length - (i + 1 )])
12
+ return false
13
+ return true ;
14
+ }
15
+
16
+
17
+ fun String.normalize (): String {
18
+ val nfdNormalizedString = Normalizer .normalize(this , Normalizer .Form .NFD )
19
+ return Pattern
20
+ .compile(" \\ p{InCombiningDiacriticalMarks}+" )
21
+ .matcher(nfdNormalizedString)
22
+ .replaceAll(" " )
23
+ .toLowerCase()
24
+ .replace(" " , " " )
25
+
26
+ }
Original file line number Diff line number Diff line change
1
+ package other
2
+
3
+ import org.junit.Assert.assertTrue
4
+ import org.junit.Test
5
+
6
+ class PalindromeTest {
7
+
8
+ @Test
9
+ fun testPalindromePortuguesePhrase () {
10
+ val text = " A mãe te ama"
11
+ assertTrue(isPalindrome(text))
12
+ }
13
+
14
+ @Test
15
+ fun testPalindromeEnglishPhrase () {
16
+ val text = " Mr Owl ate my metal worm"
17
+ assertTrue(isPalindrome(text))
18
+ }
19
+
20
+ @Test
21
+ fun testPalindromeName () {
22
+ val text = " Hannah"
23
+ assertTrue(isPalindrome(text))
24
+ }
25
+ }
You can’t perform that action at this time.
0 commit comments