public int contains(String src, String pattern) { if (src == null || pattern == null) { return -1; } int lengthSrc = src.length(); int lengthPattern = pattern.length(); if (lengthPattern > lengthSrc) { return -1; } char[] srcChars = src.toCharArray(); char[] patternChars = pattern.toCharArray(); int i = 0; int j = 0; while (i < lengthSrc) { while (j < lengthPattern && i < lengthSrc && srcChars[i] == patternChars[j]) { i++; j++; } if (j == lengthPattern) { return i - lengthPattern; } i = i - j + 1; j = 0; } return -1; } public static void main(String[] orgs) { BF bf = new BF(); System.out.println(bf.contains("123123445233423", "4452")); }