Pregunta de entrevista de Salesforce

Find the first index of the substring. Condition: Do not use java library function or regular expressions. And measure the performance of your implementation with the standard java library function. Examples: String 1: “abcdefg” String 2: “bcd” Should return 1 String 1: “abcdefg” String 2: “x” Should return -1

Respuestas de entrevistas

Anónimo

22 may 2012

public class ComputeSubString { String s1 = "abqqqqqqqbcdef"; String s2 = "xbcd"; boolean found = true; static int pos = -1; public static void main(String s[]) { new ComputeSubString().start(); System.out.println(pos); } private void start() { for (int i = 0; i s1.length() || s1.charAt(index + i) != s2.charAt(i)) { return false; } } return true; } }

1

Anónimo

28 may 2012

Is this too naive? Why not KMP?

1

Anónimo

12 oct 2012

@fireHydrant: In your case there will be a small logical error.. You code does not work in case of two string "internnet" and "net" Since in your code you keep on checking next character if there is a match. Bur what if the string is not a complete match? It never checks first value again.. Example: internnet and net: when it comes to nnet part: Your codes output: i=5 m = n t = n i=6 // here is the catch m = n t = e i=7 m = e t = n i=8 m = t t = n I modified your code and the correct code is: public static void main(String[] args) { String m = "internnet"; String t = "net"; int i, j = 0; for (i = 0; i 0){ j=0; i--; } else { j = 0; //i--; } } if (j == t.length()) { System.out.println("Yes.. A match "+(i-t.length())); } } Hope this clears doubt..

1

Anónimo

11 ene 2013

static int ReturnFirstOccurance(string mainString, string subString) { int len = mainString.Length; for (int i = 0; i < len; i++) { if (mainString[i] == subString[0]) { if (mainString[i+1] == subString[1]) { if (mainString[i + 1] == subString[1]) { return i; } } } } return -1; }

Anónimo

18 feb 2013

for (i=0; i< s.length - s1.length; i++) { for (j=0; j

Anónimo

18 feb 2013

This one is neater: for (i=0; i< s.length - s1.length; i++) { j = 0; while (j < s1.length && s1.charAt(j) == s.charAt[i+j]) j++; if (j == s1.length) return i; } return -1;

Anónimo

20 abr 2014

this can be done either by recursion or by for loop. for ForLoop: public static int indexOf(String str, String subStr) { if (str == null || str.length() == 0 || subStr == null || subStr.length() == 0) { return -1; } int i = 0, j = 0; while (i 0) { i -= j; j = 0; } ++i; } } if (j == subStr.length()) { return i - j; } return -1; } the problem with the other implementation are that they will miss the following: indexOf("internnntood", "nnt");

Anónimo

15 mar 2016

public boolean strStr(String haystack, String needle) { char[] big = haystack.toCharArray(); char[] small = needle.toCharArray(); if (big.length == 0 || small.length == 0 || big.length < small.length) { return false; } char firstChar = small[0]; //find the first character of needle in the haystack by doing linear scan for (int j = 0; j < big.length; j++) { if (big[j] == firstChar) { //check if remaining consecutive characters match continuously if (matchRest(big, small, j + 1)) { System.out.println(j); // matched at position j return true; } } } // sorry no match return false; } private boolean matchRest(char[] big, char[] small, int sBig) { int i, j; // always start from position 1 of needle since position 0 is guranteed to be matched // start with position passed for haystack and make sure to stop before either buffers runs out for (i = 1, j = sBig; (i < small.length) && (j < big.length); i++, j++) { if (big[j] != small[i]) { return false; } } // if tail of haystack has a subset of needle then its not a match if (j == big.length && i < small.length) { return false; } else { return true; } }

Anónimo

20 mar 2016

int strStr(String haystack, String needle) { int l1 = haystack.length(), l2 = needle.length(); if(l1 < l2) return -1; else if(l2 == 0) return 0; int threshold = l1 - l2 for(int i = 0; i < threshold; i++) { if(haystack.substring(i, i + l2).equals(needle)) { return i; } } return -1; }

Anónimo

21 oct 2016

C# solution: using System; namespace StringPractice { class Program { static void Main(string[] args) { //string a = "internenenenenet"; //string b = "net"; //string a = "abqqqqqqqbcdef"; //string b = "xbcd"; //string a = "internnet"; //string b = "net"; string a = "internnntood"; string b = "nnt"; Console.WriteLine(findFirstIndexOfSubString(a, b)); } /// /// Function returns first index of the substring. /// /// source string where to search for substring /// sub string of superString /// starting index of superString, where subString started, otherwise -1 public static int findFirstIndexOfSubString(string superString, string subString) { // Return -1 if either of a or b are null or empty string if(string.IsNullOrEmpty(superString) || string.IsNullOrEmpty(subString)) { return -1; } int i = 0, j = 0; // Loop through until end of superString. while (i

Anónimo

2 oct 2012

Here's a more elegant solution: public static void main(String[] args) { String m = "internet"; String t = "net"; int i, j = 0; for (i = 0; i < m.length() && j < t.length(); ++i) { if (m.charAt(i) == t.charAt(j)) { ++j; } else { j = 0; } } if (j == t.length()) { System.out.println(i+1-t.length()); } }

2

Anónimo

25 oct 2012

Find the answer here: http://programmingpassionforjava.blogspot.com/2012/10/find-first-index-of-substring.html

Anónimo

24 oct 2012

public static int indexOf(String str1, String str2) { for (int i = 0; i <= str1.length() - str2.length(); i++) { if (equals(str1, str2, i, 0)) { return i; } } return -1; } public static boolean equals(String str1, String str2, int idx1, int idx2) { while (idx1 < str1.length() && idx2 < str2.length() && str1.charAt(idx1++) == str2.charAt(idx2++)) { } return idx2 == str2.length(); }