Advertisement
JeffGrigg

Untitled

Dec 25th, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.09 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class BeautifulString20171224Test extends TestCase {
  7.  
  8.     public static void main(String[] args) {
  9.         //  System.out.println("enter a string");
  10.         Scanner scan = new Scanner(System.in);
  11.         String str1 = scan.next();
  12.         if (str1.length() < 100000) {
  13.             String str2 = makeBeautiful(str1);
  14.             if (str2 != null) {
  15.                 System.out.println("YES");
  16.                 System.out.println(str2);
  17.             } else
  18.                 System.out.println("NO");
  19.         }
  20.  
  21.     }
  22.  
  23.     private static String makeBeautiful(String original) {
  24.         if (original.length() % 2 != 0) {
  25.             return null;    // Strings of odd length always "fail" on the middle character.
  26.         }
  27.  
  28.         if (isBeautiful(original)) {
  29.             return reverse(original);   // Is already "beautiful."
  30.         }
  31.  
  32.         final String sorted = sortChars(original);
  33.         final int indexRightOfCenter = original.length() / 2;
  34.         if (sorted.charAt(0) == sorted.charAt(indexRightOfCenter) || sorted.charAt(indexRightOfCenter - 1) == sorted.charAt(sorted.length() - 1)) {
  35.             return null;    // One character fills more than half of the array.
  36.         }
  37.  
  38.         final CharSequence leftHalf = sorted.subSequence(0, indexRightOfCenter);
  39.         final CharSequence rightHalf = sorted.subSequence(indexRightOfCenter, sorted.length());
  40.         return leftHalf + reverse(rightHalf);
  41.     }
  42.  
  43.     private static boolean isBeautiful(final String value) {
  44.         if (value.length() == 0) {
  45.             return true;
  46.         }
  47.         final int centerIndex = value.length() / 2;
  48.         for (int charIdx = 0; charIdx <= centerIndex; ++charIdx) {
  49.             if (value.charAt(charIdx) == value.charAt(value.length() - charIdx - 1)) {
  50.                 return false;   // Found duplicate character.
  51.             }
  52.         }
  53.         return true;    // No duplicates found.
  54.     }
  55.  
  56.     private static String reverse(final CharSequence value) {
  57.         return new StringBuilder(value).reverse().toString();
  58.     }
  59.  
  60.     private static String sortChars(final String original) {
  61.         final char[] characters = original.toCharArray();
  62.         Arrays.sort(characters);
  63.         return new String(characters);
  64.     }
  65.  
  66.     public void testMultipleValues() {
  67.         assertIsBeautiful("");
  68.         isNotBeautifiable("a");
  69.         isNotBeautifiable("aa");
  70.         assertIsBeautiful("ab");
  71.         isNotBeautifiable("abc");
  72.         assertIsBeautiful("aabb");
  73.         isBeautifiable("abba");
  74.         isBeautifiable("abbc");
  75.         isNotBeautifiable("abaa");
  76.         isNotBeautifiable("abcde");
  77.         assertIsBeautiful("aaabbb");
  78.         isNotBeautifiable("abaaab");
  79.         isBeautifiable("abccba");
  80.         isBeautifiable("aabbba");
  81.         isNotBeautifiable("abcdefghijklmnopqrstuvwxyza");
  82.         isBeautifiable(copies(80, "abcdefghijklmnopqrstuvwxyz") + "aa");
  83.         isBeautifiable(copies(3846, "abcdefghijklmnopqrstuvwxyz") + "aa");
  84.         isBeautifiable(copies(4000, "abcdefghijklmnopqrstuvwxyz") + "aa");
  85.         isNotBeautifiable("abbb");
  86.         isNotBeautifiable("aabbbb");
  87.     }
  88.  
  89.     private String copies(final int numberOfCopies, final String value) {
  90.         final StringBuilder stringBuilder = new StringBuilder();
  91.         for (int counter = 0; counter < numberOfCopies; ++counter) {
  92.             stringBuilder.append(value);
  93.         }
  94.         return stringBuilder.toString();
  95.     }
  96.  
  97.     private static void assertIsBeautiful(final String beautifulStringValue) {
  98.         assertEquals("The String value <" + beautifulStringValue + "> is beautiful;", true, isBeautiful(beautifulStringValue));
  99.         assertEquals("The String value <" + beautifulStringValue + "> is beautiful;", reverse(beautifulStringValue), makeBeautiful(beautifulStringValue));
  100.     }
  101.  
  102.     private static void isNotBeautifiable(final String original) {
  103.         assertEquals("The String value <" + original + "> is NOT beautiful;", false, isBeautiful(original));
  104.         assertEquals("The String value <" + original + "> is NOT beautiful;", null, makeBeautiful(original));
  105.     }
  106.  
  107.     private static void isBeautifiable(final String original) {
  108.         assertEquals("The String value <" + original + "> is NOT beautiful;", false, isBeautiful(original));
  109.  
  110.         final String butified = makeBeautiful(original);
  111.         if (butified == null) {
  112.             fail("The String value <" + original + "> is should be beautifyable.");
  113.         } else {
  114.             assertEquals("The String value <" + original + "> is should be beautifyable, as value <" + butified + ">;",
  115.                     true, isBeautiful(butified));
  116.             assertEquals("The String value <" + original + "> and butified value <" + butified + "> should be the same length;",
  117.                     original.length(), butified.length());
  118.             assertEquals("The String value <" + original + "> and butified value <" + butified + "> should be the same length;",
  119.                     sortChars(original), sortChars(butified));
  120.         }
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement