-
Notifications
You must be signed in to change notification settings - Fork 0
/
ques2.java
40 lines (31 loc) · 1.2 KB
/
ques2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// you are given a string , return a new string with the string being sorted in an alphabetical order
import java.util.*;
public class ques2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(sortedd(str) + " with nlogn iterations");
System.out.println(sortedorder(str) +" with 26n iterations");
}
static String sortedd(String str) {
char[] arr = str.toCharArray();
Arrays.sort(arr); //nlogn number of iterations , which is less then n square iterations
return new String(arr);
}
// another method to do this with less iterations , without directly sorting the array
// count sort approach (only works for smaller ranges of data)
static String sortedorder(String str) {
int[] freq = new int[26];
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
freq[ch - 'a']++;
}
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i<26 ; i++){
for(int j = 0 ; j <freq[i];j++){
sb.append((char)(i+'a'));
}
}
return sb.toString();
}
}