[Programming Question] Even String Analysis Code
Collection of Programming Questions for National Unified Written Mock Test (5th) for School Recruitment 2017
Time limit: 1 second Space limit: 32768K
If a string consists of two identical strings concatenated, Just call this string an even string。 for example"xyzxyz" harmony"aaaaaa" It's an even string., nevertheless"ababab" harmony"xyzxy" but it's not。 The bull now gives you an even string s containing only lowercase letters, you can delete 1 and or more characters from the end of the string s to ensure that the string is still an even string after the deletion, and the bull wants to know what the longest even string length is after the deletion. Input Description: The input consists of a string s, length(2 ≤ length ≤ 200), ensuring that s is an even string and consists of lowercase letters
Output Description: Outputs an integer that indicates the length of the longest even string that can be obtained after deletion. Guaranteed non-zero solution for test data
Enter example 1: abaababaab
Output example 1: 6
Simple question, the title makes the idea very clear. Start deleting from the last character to see if it is an even string, if it is, it is returned directly, if not, continue deleting.
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); in.close(); System.out.println(helper(s)); } private static int helper(String s) { for(int i=s.length()-1;i>0;i--) { if(isString(s.substring(0, i))) return i; } return 1; } private static boolean isString(String s) { String front = s.substring(0, s.length()/2); String end = s.substring(s.length()/2, s.length()); return front.equals(end); } }