SRM 428 DIV2 Easy(250) - ThePalindrome
回文を作る最小の文字列長を求める問題。
Problem Statement
John and Brus are studying string theory at the university. Brus likes palindromes very much. A palindrome is a word that reads the same forward and backward. John would like to surprise Brus by taking a String s, and appending 0 or more characters to the end of s to obtain a palindrome. He wants that palindrome to be as short as possible. Return the shortest possible length of a palindrome that John can generate.
Definition
- Class: ThePalindrome
- Method: find
- Parameters: String
- Returns: int
- Method signature: int find(String s) (be sure your method is public)
Limits
- Time limit (s): 2.000
- Memory limit (MB): 64
Constraints
- s will contain between 1 and 50 characters, inclusive.
- Each character of s will be a lowercase letter ('a' - 'z').
Solution
public class ThePalindrome { public int find(String s) { for (int i = s.length(); i < 2 * s.length(); i++) { boolean isPalidromes = true; for (int j = 0; j < s.length(); j++) { if ((i - j - 1) < s.length() && s.charAt(j) != s.charAt(i - j - 1)) { isPalidromes = false; break; } } if (isPalidromes) return i; } return -1; } }