Wednesday, August 26, 2015

Manny's password Problem

Danny has a possible list of passwords of Manny's facebook account. All passwords length is odd. But Danny knows that Manny is a big fan of palindromes. So, his password and reverse of his password both should be in the list.

You have to print the length of Manny's password and it's middle character.

Note : The solution will be unique.

INPUT
The first line of input contains the integer N, the number of possible passwords.
Each of the following N lines contains a single word, its length being an odd number greater than 2 and lesser than 14. All characters are lowercase letters of the English alphabet.

OUTPUT
The first and only line of output must contain the length of the correct password and its central letter.

CONSTRAINTS
1 ≤ N ≤ 100

Sample Input(Plaintext Link)
4
abc
def
feg
cba

Sample Output(Plaintext Link)
3 b



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
import java.io.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
  Scanner scan = new Scanner(System.in);
  int cases = scan.nextInt();
  
  String [] data = new String[cases];
  for(int x=0;x<cases;x++){
   data[x] = scan.next();
  }
  String mid="";
  for(int x=0;x<data.length-1;x++){
   for(int y=1;y<data.length;y++){
    if(palin(data[x]).equals(data[y]))
     mid = data[x].length()+" "+data[x].substring(data[x].length() / 2, data[x].length() / 2 + 1 );
     
   }
  }
  
  System.out.print(mid);
    }
    
    public static String palin(String x){
     String value = "";
     
     value = new StringBuffer(x).reverse().toString();
      
     return value;
    }
}

1 comment: