Results 1 to 3 of 3

Thread: Palindrome Problem - Minimum number of characters to make a text palindrome

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Palindrome Problem - Minimum number of characters to make a text palindrome

    So, I'm having a very difficult task here. I need to analyze a certain text and find the minimum number of characters it needs to become a palindrome. The text will not have any special characters or numbers, just alphabet letters. Now, I've made some progression and have tried a lot of situations correctly, but in some situations I've gotten wrong outputs on the MINIMUM number of characters needed. I've tested the below texts:

    AAABBBBCCCCC - AAABBBBCCCCCBBBBAAA ✓ 7
    CCCCCBBBBAAA - AAABBBBCCCCCBBBBAAA ✓ 7
    BBBBCCCCCAAA - AAABBBBCCCCCBBBBAAA ✓ 7
    FASLLI - FAS-I-LLI-SAF ✓ 4
    ARBEN - ARBEN-EBRA ✓ 4
    ARBENA - ARBEN-EBR-A ✓ 3
    ARBANA - ARBAN-ABR-A ✓ 3
    ALL - ALL-A ✓ 1
    ALLLL - ALLLL-A ✓ 1
    BAOUB - BAOU-OA-B ✓ 2
    ASDRFFF - ASDRFFF-DRSA ✓ 4
    AAABBB - AAABBB-AAA / BBB-AAABBB ✓ 3
    ARBENARBEN - ARBENARBEN-EBRANEBRA ✓ 9
    ALLALL - ALLALL-A WRONG , outputs "4" when all there's needed is 1.
    AAABBBBAAABBBBCCCCC - CCCCCBBBB-AAABBBBAAABBBBCCC WRONG, outputs 14 when all there's needed is 9 chars. In this case there should be noted that there are 2 palindromes within the text (as in substrings which may have something to do with the wrong output that I get, but I can't find what it is).

    If anyone can help me with what is going wrong with the 2 cases in which the output is wrong I would appreciate it very very much. My code is below.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    4.  
    5.         Dim counter As Integer = 0, counter2 As Integer = 0, temp As Integer = 0
    6.         Dim Half As Integer
    7.         Dim Test As Boolean = False
    8.         RichTextBox1.Text = RichTextBox1.Text.Replace(" ", "")
    9.         RichTextBox1.Text = RichTextBox1.Text.Replace(".", "")
    10.         RichTextBox1.Text = RichTextBox1.Text.Replace(vbLf, "")
    11.  
    12.         Dim j As Integer = RichTextBox1.Text.Length
    13.  
    14.         If RichTextBox1.Text.Length Mod 2 Then
    15.             ' just for help to test MsgBox("You've put an odd no. of Chars.")
    16.             Half = Math.Floor((RichTextBox1.Text.Length - 1) / 2)
    17.  
    18.             For i = 0 To Half - 1
    19.  
    20.                 For j = RichTextBox1.Text.Length - 1 To 0 Step -1
    21.                     If i < Half Then
    22.                         If RichTextBox1.Text.Chars(i) = RichTextBox1.Text.Chars(j) Then
    23.                             MsgBox(RichTextBox1.Text.Chars(i) & RichTextBox1.Text.Chars(j))
    24.                             counter = counter + 2
    25.                         Else
    26.                             MsgBox("Same chars at respective places are " & counter)
    27.                             Test = True
    28.                             MsgBox("Test became true")
    29.                             Exit For
    30.                         End If
    31.                         i += 1
    32.                     Else
    33.                         'i += 1
    34.                     End If
    35.                     ' i = i + 1
    36.                 Next
    37.  
    38.                 If Test = True Then Exit For
    39.  
    40.             Next
    41.  
    42.         Else
    43.           ' just for help to test  MsgBox("You've put an even no. of Chars.")
    44.             Half = Math.Floor((RichTextBox1.Text.Length) / 2)
    45.             MsgBox(Half)
    46.  
    47.             For i = 0 To Half - 1
    48.  
    49.                 For j = RichTextBox1.Text.Length - 1 To Half Step -1
    50.                     If i < Half Then
    51.                         If RichTextBox1.Text.Chars(i) = RichTextBox1.Text.Chars(j) Then
    52.                             '            MsgBox(RichTextBox1.Text.Chars(i) & RichTextBox1.Text.Chars(j))
    53.                             counter = counter + 2
    54.  
    55.                         Else
    56.                             MsgBox("Same chars at respective places are " & counter)
    57.                             Test = True
    58.                             MsgBox("Test became true")
    59.                             Exit For
    60.                             '   j -= 1
    61.                         End If
    62.                         i += 1
    63.                         '   Exit For
    64.  
    65.                     End If
    66.  
    67.                 Next
    68.  
    69.                 If Test = True Then Exit For
    70.  
    71.             Next
    72.  
    73.         End If
    74.  
    75.         If counter / 2 = Half Then
    76.             MsgBox("This text is a palindrome, it doesn't need any additional character.")
    77.         Else
    78.  
    79.             '     For j = RichTextBox1.Text.Length - 1 To 1 Step -1
    80.             'If RichTextBox1.Text.Chars(j) = RichTextBox1.Text.Chars(j - 1) Then
    81.  
    82.            For i = 0 To RichTextBox1.Text.Length - 2
    83.                 If RichTextBox1.Text.Chars(i + 1) = RichTextBox1.Text.Chars(i) Then
    84.                     counter2 = counter2 + 1
    85.                     MsgBox("The no. of same straight chars atm is " & counter2)
    86.                     If temp <= counter2 Then
    87.                         temp = counter2
    88.                         MsgBox("Biggest same straight char no. is " & temp)
    89.                     End If
    90.  
    91.                     MsgBox(counter2)
    92.                 Else
    93.                     counter2 = 0
    94.                     End If
    95.  
    96.             Next
    97.             If counter2 <= temp Then
    98.                 counter2 = temp
    99.             End If
    100.             MsgBox("Temp =" & temp)
    101.  
    102.             MsgBox("This text is not a palindrome, it needs " & (RichTextBox1.Text.Length - 1 - counter - temp) & " chars to become one.")
    103.         End If
    104.         'MsgBox(counter)
    105.  
    106.     End Sub
    107.  
    108. End Class

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Palindrome Problem - Minimum number of characters to make a text palindrome

    This works for every case you add characters to the beginning or to the end of the string in direct and reverse order. This won't be working if you wish to include cases when characters are added in the middle. In fact, I think it'll be too many iterations to check all the possibilities.
    For example, this will return 4 for BAOUB

    vb.net Code:
    1. '
    2.     Private Function CountCharsToPalyndrome(ByVal text As String) As Integer
    3.         Dim addeddirect As String = text
    4.         Dim addedreverse As String = New String(text.AsEnumerable.Reverse().ToArray())
    5.         Dim returnval As Integer = addeddirect.Length
    6.         ' Direct order, cutting from the end
    7.         Dim teststring As String
    8.         Dim charscut As Integer = 1
    9.         Do While charscut < text.Length
    10.             ' Direct order, cutting from the end
    11.             teststring = addeddirect.Substring(0, addeddirect.Length - charscut)
    12.             If IsPalyndrome(teststring & text) OrElse IsPalyndrome(text & teststring) Then
    13.                 returnval = teststring.Length
    14.             End If
    15.             ' Direct order, cutting from the beginning
    16.             teststring = addeddirect.Substring(charscut)
    17.             If IsPalyndrome(teststring & text) OrElse IsPalyndrome(text & teststring) AndAlso
    18.                     returnval > teststring.Length Then
    19.                 returnval = teststring.Length
    20.             End If
    21.             ' reverse order, cutting from the end
    22.             teststring = addedreverse.Substring(0, addedreverse.Length - charscut)
    23.             If IsPalyndrome(teststring & text) OrElse IsPalyndrome(text & teststring) AndAlso
    24.                     returnval > teststring.Length Then
    25.                 returnval = teststring.Length
    26.             End If
    27.             ' reverse order, cutting from the beginning
    28.             teststring = addedreverse.Substring(charscut)
    29.             If IsPalyndrome(teststring & text) OrElse IsPalyndrome(text & teststring) AndAlso
    30.                     returnval > teststring.Length Then
    31.                 returnval = teststring.Length
    32.             End If
    33.             charscut += 1 ' 1 less character
    34.         Loop
    35.         Return (returnval)
    36.     End Function
    37.  
    38.     Private Function IsPalyndrome(ByVal text As String) As Boolean
    39.         Dim firsthalf As String = String.Empty
    40.         Dim middle As String = String.Empty
    41.         Dim lasthalf As String = String.Empty
    42.  
    43.         ' Determine if we have odd or even number of characters
    44.         If (text.Length Mod 2) = 0 Then
    45.             firsthalf = text.Substring(0, text.Length \ 2)
    46.             lasthalf = text.Substring(text.Length \ 2)
    47.         Else
    48.             firsthalf = text.Substring(0, (text.Length - 1) \ 2)
    49.             middle = text.Substring(firsthalf.Length, 1)
    50.             lasthalf = text.Substring((firsthalf.Length + 1))
    51.         End If
    52.         Return New String(lasthalf.AsEnumerable.Reverse().ToArray()).Equals(firsthalf)
    53.     End Function


    Test results (slightly different due to the limitations described above):
    Code:
    AAABBBBCCCCC: 7
    CCCCCBBBBAAA: 7
    BBBBCCCCCAAA: 8
    FASLLI: 5
    ARBEN: 4
    ARBENA: 5
    ARBANA: 3
    ALL: 1
    ALLLL: 1
    BAOUB: 4
    ASDRFFF: 4
    AAABBB: 3
    ARBENARBEN: 9
    ALLALL: 1
    AAABBBBAAABBBBCCCCC: 9
    Hope this helps...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Re: Palindrome Problem - Minimum number of characters to make a text palindrome

    Quote Originally Posted by cicatrix View Post
    This works for every case you add characters to the beginning or to the end of the string in direct and reverse order. This won't be working if you wish to include cases when characters are added in the middle. In fact, I think it'll be too many iterations to check all the possibilities.
    For example, this will return 4 for BAOUB

    vb.net Code:
    1. '
    2.     Private Function CountCharsToPalyndrome(ByVal text As String) As Integer
    3.         Dim addeddirect As String = text
    4.         Dim addedreverse As String = New String(text.AsEnumerable.Reverse().ToArray())
    5.         Dim returnval As Integer = addeddirect.Length
    6.         ' Direct order, cutting from the end
    7.         Dim teststring As String
    8.         Dim charscut As Integer = 1
    9.         Do While charscut < text.Length
    10.             ' Direct order, cutting from the end
    11.             teststring = addeddirect.Substring(0, addeddirect.Length - charscut)
    12.             If IsPalyndrome(teststring & text) OrElse IsPalyndrome(text & teststring) Then
    13.                 returnval = teststring.Length
    14.             End If
    15.             ' Direct order, cutting from the beginning
    16.             teststring = addeddirect.Substring(charscut)
    17.             If IsPalyndrome(teststring & text) OrElse IsPalyndrome(text & teststring) AndAlso
    18.                     returnval > teststring.Length Then
    19.                 returnval = teststring.Length
    20.             End If
    21.             ' reverse order, cutting from the end
    22.             teststring = addedreverse.Substring(0, addedreverse.Length - charscut)
    23.             If IsPalyndrome(teststring & text) OrElse IsPalyndrome(text & teststring) AndAlso
    24.                     returnval > teststring.Length Then
    25.                 returnval = teststring.Length
    26.             End If
    27.             ' reverse order, cutting from the beginning
    28.             teststring = addedreverse.Substring(charscut)
    29.             If IsPalyndrome(teststring & text) OrElse IsPalyndrome(text & teststring) AndAlso
    30.                     returnval > teststring.Length Then
    31.                 returnval = teststring.Length
    32.             End If
    33.             charscut += 1 ' 1 less character
    34.         Loop
    35.         Return (returnval)
    36.     End Function
    37.  
    38.     Private Function IsPalyndrome(ByVal text As String) As Boolean
    39.         Dim firsthalf As String = String.Empty
    40.         Dim middle As String = String.Empty
    41.         Dim lasthalf As String = String.Empty
    42.  
    43.         ' Determine if we have odd or even number of characters
    44.         If (text.Length Mod 2) = 0 Then
    45.             firsthalf = text.Substring(0, text.Length \ 2)
    46.             lasthalf = text.Substring(text.Length \ 2)
    47.         Else
    48.             firsthalf = text.Substring(0, (text.Length - 1) \ 2)
    49.             middle = text.Substring(firsthalf.Length, 1)
    50.             lasthalf = text.Substring((firsthalf.Length + 1))
    51.         End If
    52.         Return New String(lasthalf.AsEnumerable.Reverse().ToArray()).Equals(firsthalf)
    53.     End Function


    Test results (slightly different due to the limitations described above):
    Code:
    AAABBBBCCCCC: 7
    CCCCCBBBBAAA: 7
    BBBBCCCCCAAA: 8
    FASLLI: 5
    ARBEN: 4
    ARBENA: 5
    ARBANA: 3
    ALL: 1
    ALLLL: 1
    BAOUB: 4
    ASDRFFF: 4
    AAABBB: 3
    ARBENARBEN: 9
    ALLALL: 1
    AAABBBBAAABBBBCCCCC: 9
    Hope this helps...
    I'll see if I can find how it works with the cases in which my output comes wrong, since yours comes right, so thank you very much, let's hope I can mix things in.


    Wow, I actually mixed things, my code with yours. I check the output of my code and yours, and I print whomever has the smalles value, therefore when your code is mistaken and mine is right I print out my output and vice-versa. So for the texts I've studied, everything is fine.
    I need to find more different examples and check them to find any possible cases where my code could be wrong since yours is very strict and I know when it is wrong, but still THANK YOU SO MUCH!
    Last edited by Legjendat; Apr 17th, 2012 at 06:51 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width