Monday, January 16, 2012

Interview Street/String Similarity


/* Enter your code here. Read input from STDIN. Print output to STDOUT */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Solution {

/**
* @author manish
*/

public static void main(String[] args)
{
int no=0;
String[] baseArray;
int[] total;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
no = Integer.parseInt(br.readLine());
baseArray= new String[no];
total =new int[no];
for (int cnt=0;cnt<baseArray.length;cnt++)
{
baseArray[cnt]= br.readLine();
total[cnt] = baseArray[cnt].length();
}
for(int count = 0;count <baseArray.length;count++)
{
for (int i = 1; i < baseArray[count].length(); i++)
{
String test=baseArray[count].substring(i);  
int retVal=maxMatch(test,baseArray[count]);
total[count]= total[count]+retVal;
}
System.out.print(total[count]+"\n");
}
}
catch(Exception e)
{
//System.out.println("Problem");
}
}

private static int maxMatch(String s,String str)
{
// TODO Auto-generated method stub
int counter=0;
char[] main_String = str.toCharArray();      
char[] substring = s.toCharArray();
for (int i = 0; i < substring.length; i++) {
if(substring[i]==main_String[i])
{
counter++;
}
else
return counter;
}
return counter;
}
}

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete