-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicography.java
More file actions
80 lines (67 loc) · 2.6 KB
/
Copy pathLexicography.java
File metadata and controls
80 lines (67 loc) · 2.6 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.*;
public class quesone
{
public static void main(String args[])
{
Scanner sin=new Scanner(System.in);
String st=sin.next();
int n=sin.nextInt();
int count=0;
int N = st.length();
// Initialize a prefix sum array
int []pref = new int[N];
// Loop through the string to
// create the prefix sum array
for (int i = 0; i < N; i++)
{
// Store 1 at the index
// if it is a vowel
if (st.charAt(i) == 'a' ||
st.charAt(i) == 'e' ||
st.charAt(i) == 'i' ||
st.charAt(i) == 'o' ||
st.charAt(i) == 'u')
pref[i] = 1;
// Otherwise, store 0
else
pref[i] = 0;
// Process the prefix array
if (i != 0)
pref[i] += pref[i - 1];
}
// Initialize the variable to store
// maximum count of vowels
int maxCount = pref[n - 1];
// Initialize the variable
// to store substring
// with maximum count of vowels
String res = st.substring(0, n);
// Loop through the prefix array
for (int i = n; i < N; i++)
{
// Store the current
// count of vowels
int currCount = pref[i] -
pref[i - n];
// Update the result if current count
// is greater than maximum count
if (currCount > maxCount)
{
maxCount = currCount;
res = st.substring(i - n + 1,
i + 1);
}
// Update lexicographically smallest
// substring if the current count
// is equal to the maximum count
else if (currCount == maxCount)
{
String temp = st.substring(i - n + 1,
i + 1);
if (temp.compareTo(res) < 0)
res = temp;
}
}
System.out.println(res);
}
}