Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- import java.util.Arrays;
- import java.util.Scanner;
- public class BeautifulString20171224Test extends TestCase {
- public static void main(String[] args) {
- // System.out.println("enter a string");
- Scanner scan = new Scanner(System.in);
- String str1 = scan.next();
- if (str1.length() < 100000) {
- String str2 = makeBeautiful(str1);
- if (str2 != null) {
- System.out.println("YES");
- System.out.println(str2);
- } else
- System.out.println("NO");
- }
- }
- private static String makeBeautiful(String original) {
- if (original.length() % 2 != 0) {
- return null; // Strings of odd length always "fail" on the middle character.
- }
- if (isBeautiful(original)) {
- return reverse(original); // Is already "beautiful."
- }
- final String sorted = sortChars(original);
- final int indexRightOfCenter = original.length() / 2;
- if (sorted.charAt(0) == sorted.charAt(indexRightOfCenter) || sorted.charAt(indexRightOfCenter - 1) == sorted.charAt(sorted.length() - 1)) {
- return null; // One character fills more than half of the array.
- }
- final CharSequence leftHalf = sorted.subSequence(0, indexRightOfCenter);
- final CharSequence rightHalf = sorted.subSequence(indexRightOfCenter, sorted.length());
- return leftHalf + reverse(rightHalf);
- }
- private static boolean isBeautiful(final String value) {
- if (value.length() == 0) {
- return true;
- }
- final int centerIndex = value.length() / 2;
- for (int charIdx = 0; charIdx <= centerIndex; ++charIdx) {
- if (value.charAt(charIdx) == value.charAt(value.length() - charIdx - 1)) {
- return false; // Found duplicate character.
- }
- }
- return true; // No duplicates found.
- }
- private static String reverse(final CharSequence value) {
- return new StringBuilder(value).reverse().toString();
- }
- private static String sortChars(final String original) {
- final char[] characters = original.toCharArray();
- Arrays.sort(characters);
- return new String(characters);
- }
- public void testMultipleValues() {
- assertIsBeautiful("");
- isNotBeautifiable("a");
- isNotBeautifiable("aa");
- assertIsBeautiful("ab");
- isNotBeautifiable("abc");
- assertIsBeautiful("aabb");
- isBeautifiable("abba");
- isBeautifiable("abbc");
- isNotBeautifiable("abaa");
- isNotBeautifiable("abcde");
- assertIsBeautiful("aaabbb");
- isNotBeautifiable("abaaab");
- isBeautifiable("abccba");
- isBeautifiable("aabbba");
- isNotBeautifiable("abcdefghijklmnopqrstuvwxyza");
- isBeautifiable(copies(80, "abcdefghijklmnopqrstuvwxyz") + "aa");
- isBeautifiable(copies(3846, "abcdefghijklmnopqrstuvwxyz") + "aa");
- isBeautifiable(copies(4000, "abcdefghijklmnopqrstuvwxyz") + "aa");
- isNotBeautifiable("abbb");
- isNotBeautifiable("aabbbb");
- }
- private String copies(final int numberOfCopies, final String value) {
- final StringBuilder stringBuilder = new StringBuilder();
- for (int counter = 0; counter < numberOfCopies; ++counter) {
- stringBuilder.append(value);
- }
- return stringBuilder.toString();
- }
- private static void assertIsBeautiful(final String beautifulStringValue) {
- assertEquals("The String value <" + beautifulStringValue + "> is beautiful;", true, isBeautiful(beautifulStringValue));
- assertEquals("The String value <" + beautifulStringValue + "> is beautiful;", reverse(beautifulStringValue), makeBeautiful(beautifulStringValue));
- }
- private static void isNotBeautifiable(final String original) {
- assertEquals("The String value <" + original + "> is NOT beautiful;", false, isBeautiful(original));
- assertEquals("The String value <" + original + "> is NOT beautiful;", null, makeBeautiful(original));
- }
- private static void isBeautifiable(final String original) {
- assertEquals("The String value <" + original + "> is NOT beautiful;", false, isBeautiful(original));
- final String butified = makeBeautiful(original);
- if (butified == null) {
- fail("The String value <" + original + "> is should be beautifyable.");
- } else {
- assertEquals("The String value <" + original + "> is should be beautifyable, as value <" + butified + ">;",
- true, isBeautiful(butified));
- assertEquals("The String value <" + original + "> and butified value <" + butified + "> should be the same length;",
- original.length(), butified.length());
- assertEquals("The String value <" + original + "> and butified value <" + butified + "> should be the same length;",
- sortChars(original), sortChars(butified));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement