Reputation: 149
Hello so I am currently going through the Java challenges on HackerRank and ran into an issue I can't figure out with my code.
The prompt goes as follow,
Given a string, s, and an integer, k, complete the function so that it finds the lexicographically smallest and largest substrings of length k.
Sample input:
welcometojava
3
Sample output:
ava
wel
The smallest and largest substrings are solely based on the first integer. So for my solution, I decided to create an integer array and populate it with the integer value of each character available in String s.
I figured once I sorted the array, index[0] would equal the first letter of smallest while index[s.length()-1] would equal the first letter of largest. From there it would only be a matter of concatenating each following character within the array to the corresponding smallest/largest as long as smallest/largest <= k.
With the sample input above, I am able to return "wel" for largest. smallest however is returning an empty string and I can't figure out why. I literally followed the same methodology to get the value of both strings so I think smallest should be returning a string value, regardless of whether said value is the right answer.
Here is my source code right below.
public static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
int[] temp = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
temp[i] = (int) s.charAt(i);
}
Arrays.sort(temp);
char[] charArray = s.toCharArray();
// find smallest string
for (int i = 0; i < charArray.length; i++) {
if ((int) s.charAt(i) == temp[0]) {
while (i < k) {
smallest += String.valueOf(s.charAt(i));
i++;
}
}
}
// find largest string
for (int i = 0; i < charArray.length; i++) {
if ((int) s.charAt(i) == temp[s.length()-1]) {
while (i < k) {
largest += String.valueOf(s.charAt(i));
i++;
}
}
}
return smallest + "\n" + largest;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();
int k = scan.nextInt();
scan.close();
System.out.println(getSmallestAndLargest(s, k));
}
Upvotes: 0
Views: 127
Reputation: 168
Based on what you have, I think you really need to just map the characters in such a way that you order the leading character, then return the first and last ordered characters. Below is a modification of your getSmallestAndLargest
method.
public static String getSmallestAndLargest(String s, int k) {
List<String> lexList = new ArrayList<String>();
StringBuilder word;
if(s.length() < k) {
return s;
}
for(int i = 0; i < ((s.length() + 1) - k); i++) {
int j = i;
word = new StringBuilder();
while (j < (i + k)) {
word.append(s.charAt(j));
j++;
}
lexList.add(word.toString());
}
Collections.sort(lexList);
return lexList.get(0) + "\n" + lexList.get(lexList.size()-1);
}
Upvotes: 0
Reputation: 968
For the line: (int) s.charAt(i) == temp[i], do you mean temp[0] instead of temp[i]? The if statement will only be true when one of the two strings, 'welcometojava' and 'aaceejlmootvw', have the same letter at the same index, which doesn't happen.
Upvotes: 0
Reputation: 718718
I figured once I sorted the array,
index[0]
would equal the first letter of smallest whileindex[s.length() - 1]
would equal the first letter of largest.
Well yes ... but that is not sufficient. The problem is smallest letter can appear more than once in the original string. And so can the largest letter. So when you find an instance of (say) 'a'
in the original string, you don't know if it is the start of the smallest 3-character substring, or not.
There is a simpler approach.
Hint: if you have an array of 3 character strings, how do you find the smallest and largest?
Upvotes: 1