G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等,
给pattern和word,判断是否match,
package DataStreamAverage;
public class Solution3 {
public boolean check(String s1, String s2) {
return helper(s1, 0, s2, 0);
}
public boolean helper(String s1, int i1, String s2, int i2) {
if (i1==s1.length() && i2==s2.length()) return true;
if (i1==s1.length() || i2==s2.length()) return false;
if (isDigit(s2.charAt(i2))) {
int num = 0;
while (i2<s2.length() && isDigit(s2.charAt(i2))) {
num = num*10 + (int)(s2.charAt(i2)-'0');
i2++;
}
if (i1+num > s1.length()) return false;
return helper(s1, i1+num, s2, i2);
}
else {
if (s1.charAt(i1) != s2.charAt(i2)) return false;
return helper(s1, i1+1, s2, i2+1);
}
}
public boolean isDigit(char c) {
if (c>='0' && c<='9') return true;
else return false;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution3 sol = new Solution3();
boolean res = sol.check("houskjfjjdkse", "h11e");
if (res) System.out.println("True");
else System.out.println("false");
}
}
G面经prepare: Pattern Match的更多相关文章
- LPC43xx SGPIO Pattern Match Mode
模式匹配 所有位串均具有模式匹配功能. 该功能可用于检测启动代码等.要使用该功能,则必须用需匹配的模式来对REG_SS 编程 (请注意, POS 达到零时 REG_SS 不会与 REG 交换!) M ...
- python正则表达式基础,以及pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别
正则表达式(regular expression)是一个特殊的字符序列,描述了一种字符串匹配的模式,可以用来检查一个字符串是否含有某种子字符串. 将匹配的子字符串替换或者从某个字符串中取出符合某个条件 ...
- G面经prepare: Android Phone Unlock Pattern
1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- G面经prepare: Set Intersection && Set Difference
求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- G面经prepare: Data Stream Average
给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...
- G面经prepare: Jump Game Return to Original Place
第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...
- G面经prepare: Sort String Based On Another
Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...
随机推荐
- Memory Allocation in the MySQL Server
https://dev.mysql.com/doc/internals/en/memory-allocation-mysql-server.html MySQL Internals Manual / ...
- PureBasic 读取文件中一行的两个数据例子
, "Test1.txt") ; if the file could be read, we continue... , "Test2.txt") ) = ; ...
- nrf51822裸机教程-GPIOTE
GPIO通常都会具有中断功能,上一讲的GPIO中并没有涉及到中断的相关寄存器. 51822将GPIO的中断相关做成了一个单独的模块GPIOTE,这个模块不仅提供了GPIO的中断功能,同时提供了 通过t ...
- CDH介绍
本文引用自:Cloudera 系列2:CDH介绍http://www.aboutyun.com/thread-18379-1-1.html(出处: about云开发) CDH提供: 灵活性-存储任何类 ...
- PHP运行错最有效解决办法Fatal error: Out of memory (allocated 786432) (tried to allocate 98304 bytes) in H:\freehost\zhengbao2\web\includes\lib_common.php on line 744
原文 PHP运行错最有效解决办法Fatal error: Out of memory (allocated 6029312) Fatal error: Out of memory (allocated ...
- mongo安装、备份与常见命令整理
http://zlboy888.blog.163.com/blog/static/315357072012919241104/ 1 下载安装包 官方下载地址:http://www.mongodb.o ...
- Cure---hdu5879(打表+找规律)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5879 题意:给你一个n ,求∑(1/k2), k from 1 to n; n的范围是不知道的,所以可 ...
- sqlserver 中server 函数GETDATE(),DEFAULT用法
alter table Persons add datenow date DEFAULT GETDATE() null, datetimenow datetime DEFAULT GETDATE()n ...
- debian linux下配置lnmp环境
用到哪些就安装哪些 安装配置时遇到: deb cdrom:[Debian GNU/Linux 7.1.0 _Wheezy_ - Official amd64 CD Binary-1 20130615- ...
- VI使用技巧
命令模式下: 1.YY 拷贝当前行 2.P 粘贴拷贝的内容 3.dd 删除当前行 4.输入“:set nu”,显示行号 5.输入“G”,到文件尾部.输入“gg”,到文件头部. 6.输入“/字符串”,进 ...