-
-
Save DarkCat09/ce7286f7acca79177b440bae9251e819 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.regex.*; | |
public class Main { | |
public static void main(String args[]) { | |
// init input | |
final Scanner scan = new Scanner(System.in); | |
// prompt strings | |
System.out.print("Текст: "); | |
final String text = scan.nextLine()/*.toLowerCase()*/; | |
System.out.print("Строка: "); | |
final String magic = scan.nextLine()/*.toLowerCase()*/; | |
// generate regexs based | |
// on the magic string | |
ArrayList<String> regexs = new ArrayList<String>(); | |
regexs.add(magic); | |
for (int i = 0; i < magic.length(); i++) { | |
final StringBuilder sb = new StringBuilder(magic); | |
sb.replace(i, i+1, "\\w"); | |
regexs.add(sb.toString()); | |
} | |
// check all regexs | |
ArrayList<Integer> results = new ArrayList<Integer>(); | |
for (String re : regexs) { | |
Pattern p = Pattern.compile(re); | |
Matcher m = p.matcher(text); | |
while (m.find()) { | |
results.add(m.start()); | |
} | |
} | |
// if there is no matches | |
if (results.size() == 0) { | |
System.out.println(-1); | |
System.exit(0); | |
} | |
// otherwise | |
System.out.println(Collections.min(results)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment