如何在java中搜索所有可能组合的字符串?

编程入门 行业动态 更新时间:2024-10-07 18:25:49
本文介绍了如何在java中搜索所有可能组合的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何像Android studio一样在Java中实现与给定键的所有可能组合的字符串匹配. 可以吗?任何可用的正则表达式模式.

How to implement string matching with all possible combination of given key in Java just like Android studio. does? Any regex pattern available.

推荐答案

您不需要为此使用正则表达式,因为 贪心算法可以.

You do not need a regex for this, because a greedy algorithm will do.

您可以在 O(n+p) 中将字符串与模式匹配,其中 n 是字符串的长度,p 是模式的长度,遵循一个非常简单的策略:对于模式的每个字符,查找字符串中从当前索引开始的匹配字符.如果找到匹配项,将索引向前推进,然后从模式中查找下一个字符.如果模式在字符串结束前耗尽,则匹配;否则,您没有匹配项.

You can match a string against a pattern in O(n+p), where n is the length of string and p is the length of pattern, by following a very simple strategy: for each character of the pattern, look for a matching character in the string starting at the current index. If you find a match, advance the index past it, and look for the next character from the pattern. If the pattern gets exhausted before end of string, you have a match; otherwise, you do not have a match.

public static boolean match(String s, String p) { String us = s.toUpperCase(); int i = 0; for (char c : p.toUpperCase().toCharArray()) { int next = us.indexOf(c, i); if (next < 0) { return false; } i = next+1; } return true; }

演示.

更多推荐

如何在java中搜索所有可能组合的字符串?

本文发布于:2023-11-30 07:33:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649187.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   字符串   如何在   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!