Created
July 11, 2017 15:43
-
-
Save nagamanichowdary/30c1e8d4512eb807584c3f6d04296106 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class StringSimilarity { | |
| public static int calculate(String s){ | |
| char[]arr=s.toCharArray(); | |
| int length=arr.length; | |
| int count=length; | |
| for(int i=1;i<length;i++){ | |
| int len=length-i; | |
| int j=0; | |
| for(;j<len;j++) | |
| if(arr[j]!=arr[j+i]){ | |
| break; | |
| } | |
| count+=j; | |
| } | |
| return count; | |
| } | |
| /* | |
| public static int calculate(String s){ | |
| int count=s.length(); | |
| for(int i=1;i<s.length();i++){ | |
| count+=similarCount(s,s.substring(i)); | |
| } | |
| return count; | |
| } | |
| private static int similarCount(String s, String sub){ | |
| for(int i=0;i<sub.length();i++) | |
| if(s.charAt(i)!=sub.charAt(i)) | |
| return i; | |
| return sub.length(); | |
| }*/ | |
| public static void main(String[] args) { | |
| Scanner scanner = new Scanner( System.in ); | |
| int n=scanner.nextInt(); | |
| for(int i=0;i<n;i++){ | |
| String s=scanner.next(); | |
| System.out.println(calculate(s)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment