Skip to content

Instantly share code, notes, and snippets.

@belushkin
Created March 26, 2020 18:03
Show Gist options
  • Save belushkin/51e6d7a7ce51b05237724f628df0054b to your computer and use it in GitHub Desktop.
Save belushkin/51e6d7a7ce51b05237724f628df0054b to your computer and use it in GitHub Desktop.
K-th Beautiful String
import java.util.*;
public class cf {
public static void main(String[] args) {
// Use the Scanner class
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++) {
String s = sc.nextLine();
String[] result = s.split(" ");
int a = Integer.parseInt(result[0]);
int b = Integer.parseInt(result[1]);
String str = "a";
String repeated = String.join("", Collections.nCopies(a, str));
StringBuilder sb = new StringBuilder(repeated);
if (b == 1) {
sb.setCharAt(repeated.length()-1,'b');
sb.setCharAt(repeated.length()-2,'b');
System.out.println(sb.toString());
} else {
int sum = 1;
for (int j = 2; j < a; j++) {
sum = sum + j;
if (b <= (sum)) {
sb.setCharAt(repeated.length()-(j+1),'b');
sb.setCharAt(repeated.length()-(j-(sum-b)),'b');
// System.out.println("sum:" + sum + ", j: " + j + ", b:" + b);
System.out.println(sb.toString());
break;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment