The code below is to split the categories into two continuous non-empty substrings and need to check distinct common categories which must be greater than integer k. For Example: string="abbcac", k=1, output=2 Can anyone suggest optimization of the code if possible as it already have O(n) time complexity. Can you provide brute force way to doing this question and thought process do the following code. Thanks in advance. public int findWaystoSplit(String input,int k){ int[][] count = new int[2][26]; int result = 0; for(Character c : input.toCharArray()){ count[0][c-'a']++; count[1][c-'a']++; } for(int i = 0; ik){ result++; } } return result; } public int checkCommon(int[][] count) { int common=0; for(int i = 0; i<26;i++){ if(count[1][i]>0 && count[0][i]>count[1][i]){ common++; } } return common;
The code below is to split the categories into two continuous non-empty substrings and need to check distinct common categories which must be greater than integer k.
For Example: string="abbcac", k=1, output=2
Can anyone suggest optimization of the code if possible as it already have O(n) time complexity. Can you provide brute force way to doing this question and thought process do the following code. Thanks in advance.
public int findWaystoSplit(String input,int k){
int[][] count = new int[2][26];
int result = 0;
for(Character c : input.toCharArray()){
count[0][c-'a']++;
count[1][c-'a']++;
}
for(int i = 0; i<input.length();i++){ count[1][input.charAt(i)-'a']--; int common = checkCommon(count); if(common>k){ result++; } } return result; } public int checkCommon(int[][] count) { int common=0; for(int i = 0; i<26;i++){ if(count[1][i]>0 && count[0][i]>count[1][i]){ common++; } } return common; }
Step by step
Solved in 2 steps