find the substring count from | CareerCup
find the substring count from a string without string functions in java?
Given String str = "abcdefghcde";
String find = "cde";
Count occurrences of cde in String str
If space is no issue, you can use suffix tree. Time complexity: O(query length)
Or you can use KMP algorithm it requires lesser space than suffix tree.
https://github.com/Simiopolis/careercup-java/blob/master/src/salesforce/SFCountSubstringsQ.java
public static int substringsCount(String string, String pattern) {
if(string == null || pattern == null)
return -1;
char[] str = string.toCharArray();
char[] pat = pattern.toCharArray();
int startIndex = 0;
int lastIndex = 0;
int count = 0;
while(lastIndex < str.length) {
if(lastIndex - startIndex >= pat.length || str[lastIndex] != pat[lastIndex-startIndex]) {
startIndex++;
lastIndex = startIndex;
} else if(lastIndex - startIndex == pat.length-1) {
count++;
startIndex = lastIndex+1;
}
lastIndex++;
}
return count;
}
https://github.com/Simiopolis/careercup-java/blob/master/src/salesforce/SFFindFirstOccurenceQ.java
* Find the first index of the substring without using a java library
* function or regular expressions.
public static void main(String[] args) {
System.out.println(firstIndexOfSubstring("dworldhellow world", "world"));
// Test from Glassdoor.
System.out.println(firstIndexOfSubstring("internnet", "net"));
}
public static int firstIndexOfSubstring(String s, String p) {
int firstIndex = 0;
int lastIndex = 0;
while(lastIndex < s.length()) {
// If there is an initial match, update firstIndex.
if(s.charAt(lastIndex) == p.charAt(0))
firstIndex = lastIndex;
// If we have searched beyond the length of the pattern
// or the pattern stopped matching, reset firstIndex.
else if(lastIndex-firstIndex >= p.length() || s.charAt(lastIndex) != p.charAt(lastIndex-firstIndex))
firstIndex = -1;
// If everything checks and the last character of the pattern
// is the same as the current string index, we've found
// the first occurence.
else if(s.charAt(lastIndex) == p.charAt(p.length()-1))
return firstIndex;
lastIndex++;
}
return firstIndex;
}
Read full article from find the substring count from | CareerCup
find the substring count from a string without string functions in java?
Given String str = "abcdefghcde";
String find = "cde";
Count occurrences of cde in String str
If space is no issue, you can use suffix tree. Time complexity: O(query length)
Or you can use KMP algorithm it requires lesser space than suffix tree.
https://github.com/Simiopolis/careercup-java/blob/master/src/salesforce/SFCountSubstringsQ.java
public static int substringsCount(String string, String pattern) {
if(string == null || pattern == null)
return -1;
char[] str = string.toCharArray();
char[] pat = pattern.toCharArray();
int startIndex = 0;
int lastIndex = 0;
int count = 0;
while(lastIndex < str.length) {
if(lastIndex - startIndex >= pat.length || str[lastIndex] != pat[lastIndex-startIndex]) {
startIndex++;
lastIndex = startIndex;
} else if(lastIndex - startIndex == pat.length-1) {
count++;
startIndex = lastIndex+1;
}
lastIndex++;
}
return count;
}
https://github.com/Simiopolis/careercup-java/blob/master/src/salesforce/SFFindFirstOccurenceQ.java
* Find the first index of the substring without using a java library
* function or regular expressions.
public static void main(String[] args) {
System.out.println(firstIndexOfSubstring("dworldhellow world", "world"));
// Test from Glassdoor.
System.out.println(firstIndexOfSubstring("internnet", "net"));
}
public static int firstIndexOfSubstring(String s, String p) {
int firstIndex = 0;
int lastIndex = 0;
while(lastIndex < s.length()) {
// If there is an initial match, update firstIndex.
if(s.charAt(lastIndex) == p.charAt(0))
firstIndex = lastIndex;
// If we have searched beyond the length of the pattern
// or the pattern stopped matching, reset firstIndex.
else if(lastIndex-firstIndex >= p.length() || s.charAt(lastIndex) != p.charAt(lastIndex-firstIndex))
firstIndex = -1;
// If everything checks and the last character of the pattern
// is the same as the current string index, we've found
// the first occurence.
else if(s.charAt(lastIndex) == p.charAt(p.length()-1))
return firstIndex;
lastIndex++;
}
return firstIndex;
}
Read full article from find the substring count from | CareerCup