[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

analysis

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.

code

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);
    }
}

Recommended>>
1、Is it enough to apologize for algorithmic values when the smallest mom on the internet is a selling point
2、Wacker Gold Official Launch of WCG Wacker Gold Exclusive Wallet
3、The White Paper on Full Burial Techniques for Android is back Open source all project source code
4、Charging like this is dangerous and can steal all the information from your phone in minutes
5、Chinas smart bikesharing creates a new force for global mobility

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号