Shiv and Lucky String Problem Java
The Shiv and Lucky String problem is a popular coding problem that challenges developers to create an algorithm that can find the number of substrings in a given string that contain the same number of vowels and consonants. In this article, we will discuss the Shiv and Lucky String problem in Java and provide a solution to solve it.
Problem Statement
The Shiv and Lucky String problem statement can be described as follows:
Given a string of lowercase English letters, the task is to count the number of substrings of the given string that contains equal numbers of vowels and consonants. A substring is a contiguous sequence of characters within the string.
Algorithm:
The problem requires us to find the count of substrings that contain equal numbers of vowels and consonants. Therefore, we need to traverse through all possible substrings of the given string and check if it contains equal numbers of vowels and consonants.
To solve the problem, we can follow the below approach:
- Traverse through all possible substrings of the given string.
- For each substring, count the number of vowels and consonants in it.
- If the count of vowels and consonants in the substring is equal, increment the count.
- After traversing all substrings, return the count.
- The above approach requires us to traverse through all substrings, which will result in a time complexity of O(n^2). Therefore, the problem can be solved using dynamic programming as well.
Dynamic Programming Solution
We can use dynamic programming to solve the Shiv and Lucky String problem efficiently. The approach is as follows:
- Initialize two variables, countVowels and countConsonants, with the value 0.
- Initialize an array dp[] of size n+1 with the value 0, where n is the length of the input string.
- Traverse through the input string and for each character:
- If the character is a vowel, increment the countVowels variable.
- Else, increment the countConsonants variable.
- Calculate the difference between countVowels and countConsonants, i.e., diff = countVowels - countConsonants.
- If the value of diff is between -n and n, increment the dp[diff + n] variable by 1.
Traverse through the dp[] array and calculate the number of substrings that contain equal numbers of vowels and consonants. The count can be calculated using the formula count = (dp[i] * (dp[i] - 1)) / 2, where i varies from 0 to 2n.
The time complexity of the above approach is O(n), which is more efficient than the previous approach.
ShivAndLuckyString.java
import java.util.*;
public class ShivAndLuckyString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.println(countLuckyStrings(str));
}
public static int countLuckyStrings(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '4' || str.charAt(i) == '7') {
count++;
}
}
if (count == 0) {
return -1; // no lucky strings
}
for (int len = count; len > 0; len--) {
for (int i = 0; i <= str.length() - len; i++) {
int tempCount = 0;
for (int j = i; j < i + len; j++) {
if (str.charAt(j) == '4' || str.charAt(j) == '7') {
tempCount++;
}
}
if (tempCount == len) {
return len;
}
}
}
return -1; // no lucky strings found
}
}
Input:
ababccc
Output:
-1
Explanation:
- In the main method, we first read in the input string using a Scanner object.
- We then call the countLuckyStrings method with the input string as an argument.
- The countLuckyStrings method iterates over the input string using a for loop. We stop at input.length() - 1 because we are looking at pairs of adjacent characters, and we don't want to go out of bounds.
- In each iteration of the loop, we use the substring method to extract a pair of adjacent characters from the input string. We then check if this pair of characters is a lucky string by using the contains method on the LUCKY_STRINGS set. If it is a lucky string, we increment the count variable.
- Once the loop is complete, we return the final count value, which represents the total number of lucky strings in the input string.
This is just one possible implementation of the solution to the Shiv and Lucky String problem in Java. There may be other ways to solve the problem using different algorithms or data structures.
Complexity:
The outer loop runs n times, and the middle loop runs n - len + 1 times, where len is the current length of the lucky string being checked. The inner loop also runs len times. Therefore, the overall time complexity is: O(n^3)