Home >Web Front-end >JS Tutorial >See how the master uses javascript programming to solve the black cow problem

See how the master uses javascript programming to solve the black cow problem

巴扎黑
巴扎黑Original
2017-07-20 13:32:311521browse
Problem Description

Time limit: 1 second

Space limit: 32768K

Niu Niu has become black and wants to destroy the earth. But he forgot the password to activate the Earth Destroyer. Niu Niu has a string S in his hand. Niu Niu still remembers that removing one character from S is the correct password. Please help Niu Niu find out how many times he needs to try the password at most.
As shown in the example S = "ABA", the three possible passwords are "BA", "AA", "AB".
When S = "A", the only password that Niu Niu can try is one Empty password, so output 1.
Input description:
输入包括一个字符串S,字符串长度length(1 ≤ length ≤ 50),其中都是从'A'到'Z'的大写字母。
Output description:
输出一个整数,表示牛牛最多需要尝试的密码次数。
Input Example 1:
ABA
Output example 1:
3
Solution idea:
//Single occurrence Characters
//Multiple side-by-side characters
(function mian(){
    var line = readline().split(' ');
    var arr=line[0];
    var count=0;
       for(var i=0;i<arr.length;i++){
        if(arr[i-1]!=arr[i]&&arr[i+1]!=arr[i]){
            count++;
        }else if(arr[i-1]==arr[i]&&arr[i+1]!=arr[i]){
            count++;
        }
    }     
    print(count);
})();


The above is the detailed content of See how the master uses javascript programming to solve the black cow problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn