Use Java to design a program and find the number of symmetric strings in a string]

Source: Internet
Author: User

Question requirements:

Design a program in Java to implement the symmetric number of a string, such as the string "effeghg", which contains three symmetric characters: "FF", "effe", and "GHG, so 3 is returned.

 

My idea is to traverse this string,

First, select the first character as the header and traverse the string from the end,

If the start and end of the string are the same, the intermediate string is used for recursion.

The result is obtained after recursion,

Continue to push the header back to 1 bit, and then traverse from the end of the string forward,

In this loop, when the end is equal to the header, the outermost loop is exited and the result is output.

 

Specific implementation:

  1. /**
  2. * @ Author bzwm
  3. *
  4. */
  5. Public class findpolicrystr {
  6. /**
  7. * Find the number of symmetric substrings in the string
  8. * @ Param orgstr
  9. * @ Return
  10. */
  11. Public static int findsymmetrystr (string orgstr ){
  12. // Result Initialization
  13. Int COUNT = 0;
  14. // Search when the input string is not null and the length is greater than 1; otherwise, 0 is returned directly.
  15. If (orgstr! = NULL & orgstr. Length ()> 1 ){
  16. // Obtain the length of the input string
  17. Int size = orgstr. Length ();
  18. // Index of the first character of the string
  19. Int head;
  20. // The "tail" Character index of the string from the back to the front, that is, the current character index
  21. Int current;
  22. // The first character of the string
  23. Char hstr;
  24. // The "last" character of the string from the back to the front
  25. Char CSTR;
  26. // Traverse strings from the beginning
  27. For (Head = 0; head <size; head ++ ){
  28. // Get the first character
  29. Hstr = orgstr. charat (head );
  30. // Point to the end of the input string
  31. Current = size-1;
  32. // Exit the loop when the last character index is equal to the first character index
  33. While (current> head ){
  34. // Get the last character
  35. CSTR = orgstr. charat (current );
  36. // If the start and end characters are equal, continue to judge
  37. If (hstr = CSTR ){
  38. // Retrieve the substring in the middle of the header and tail and analyze it
  39. String newstr = orgstr. substring (Head + 1, current );
  40. // Recursion is performed if the length of the substring is greater than 1
  41. If (newstr. Length ()> 1)
  42. // Recursively obtain the number of symmetric strings in this substring
  43. Count + = findsymmetrystr (newstr );
  44. // If the substring contains only one or zero characters, it indicates that the original header and tail character form a symmetric string
  45. Else
  46. Count ++;
  47. // Push the index of the last character to the first place
  48. Current --;
  49. }
  50. // If the start and end characters are not equal, the index of the end character is pushed forward by one character
  51. Else {
  52. Current --;
  53. }
  54. }
  55. }
  56. }
  57. Return count;
  58. }
  59. // Test the program
  60. Public static void main (string ARGs []) {
  61. Int COUNT = findjavasrystr ("cddcbcbeffeghg ");//
  62. System. Out. println ("handle ry string count is:" + count );
  63. }
  64. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.