您的位置:首页 > 其它

在字符串中删除特定的字符

2013-03-16 12:26 218 查看
题目:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”。

分析:这是一道微软面试题。在微软的常见面试题中,与字符串相关的题目占了很大的一部分,因为写程序操作字符串能很好的反映我们的编程基本功。

要编程完成这道题要求的功能可能并不难。毕竟,这道题的基本思路就是在第一个字符串中拿到一个字符,在第二个字符串中查找一下,看它是不是在第二个字符串中。如果在的话,就从第一个字符串中删除。但如何能够把效率优化到让人满意的程度,却也不是一件容易的事情。也就是说,如何在第一个字符串中删除一个字符,以及如何在第二字符串中查找一个字符,都是需要一些小技巧的。

首先我们考虑如何在字符串中删除一个字符。由于字符串的内存分配方式是连续分配的。我们从字符串当中删除一个字符,需要把后面所有的字符往前移动一个字节的位置。但如果每次删除都需要移动字符串后面的字符的话,对于一个长度为n的字符串而言,删除一个字符的时间复杂度为O(n)。而对于本题而言,有可能要删除的字符的个数是n,因此该方法就删除而言的时间复杂度为O(n2)。

事实上,我们并不需要在每次删除一个字符的时候都去移动后面所有的字符。我们可以设想,当一个字符需要被删除的时候,我们把它所占的位置让它后面的字符来填补,也就相当于这个字符被删除了。在具体实现中,我们可以定义两个指针(pFast和pSlow),初始的时候都指向第一字符的起始位置。当pFast指向的字符是需要删除的字符,则pFast直接跳过,指向下一个字符。如果pFast指向的字符是不需要删除的字符,那么把pFast指向的字符赋值给pSlow指向的字符,并且pFast和pStart同时向后移动指向下一个字符。这样,前面被pFast跳过的字符相当于被删除了。用这种方法,整个删除在O(n)时间内就可以完成。

接下来我们考虑如何在一个字符串中查找一个字符。当然,最简单的办法就是从头到尾扫描整个字符串。显然,这种方法需要一个循环,对于一个长度为n的字符串,时间复杂度是O(n)。

由于字符的总数是有限的。对于八位的char型字符而言,总共只有28=256个字符。我们可以新建一个大小为256的数组,把所有元素都初始化为0。然后对于字符串中每一个字符,把它的ASCII码映射成索引,把数组中该索引对应的元素设为1。这个时候,要查找一个字符就变得很快了:根据这个字符的ASCII码,在数组中对应的下标找到该元素,如果为0,表示字符串中没有该字符,否则字符串中包含该字符。此时,查找一个字符的时间复杂度是O(1)。其实,这个数组就是一个hash表。

对应于java代码,本文与原作者思路略有不同,查找字符还是采用hash表的方法,但是删除字符时略有区别,本文实现了单链表,完成哈希表的初始化后,对源字符串的字符一个一个遍历,如果该字符不在delestr中(hash[sourcestr.charAt(i)]==0),则将该字符加入到链表中,最后打印链表。

import java.util.LinkedList;
public class Test_36 {
public static void main(String[] args) {
String strsour = "they are students";
String strdele = "aeiou";
deletechar(strsour,strdele);

}
public static void deletechar(String sourcestr,String deletstr){
if(sourcestr==null||deletstr==null){
return;
}
int[] hash=new int[256];
for(int i =0;i<deletstr.length();i++){
hash[deletstr.charAt(i)]=1;
}
//LinkedList<char> list = new LinkedList<char>();
SignListchar list = new SignListchar();
for(int i =0;i<sourcestr.length();i++){
if(hash[sourcestr.charAt(i)]==0)
list.add(sourcestr.charAt(i));
}
Nodechar node = list.getHead();
while(node!=null){
System.out.print((char)node.getData());
node = node.getNext();
}
}

}
class Nodechar {
private char data;
private Nodechar next;

public Nodechar(char data, Nodechar next) {
this.data = data;
this.next = next;
}

public int getData() {
return data;
}

public void setData(char data) {
this.data = data;
}

public Nodechar getNext() {
return next;
}

public void setNext(Nodechar next) {
this.next = next;
}

}

class SignListchar {
private Nodechar head;
private Nodechar tail;
private int size;

public Nodechar getHead() {
return head;
}

public void setHead(Nodechar head) {
this.head = head;
}

public Nodechar getTail() {
return tail;
}

public void setTail(Nodechar tail) {
this.tail = tail;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public void add(char i) {
Nodechar newnode = new Nodechar(i, null);
if (head == null) {
head = newnode;
tail = newnode;
size++;
} else {
tail.setNext(newnode);
tail = newnode;
size++;
}

}
public void delete(char i){
if(head == null){
System.out.println("链表为空!");
return;
}
Nodechar pnode = head;
while(pnode.getNext().getData()!=i&&pnode!=null)
pnode=pnode.getNext();
if(pnode==null)
return;
pnode.setNext(pnode.getNext().getNext());
size--;

}
public void add(char ch,int index){
if(head == null){
System.out.println("链表为空!");
return;
}
if(index>size){
System.out.println("下标超出范围!");
return;
}
Nodechar node = head;
Nodechar newnode = new Nodechar(ch,null);
for(int i =0;i<index-1;i++){
node = node.getNext();
}
if(node.getNext()==null){
node.setNext(newnode);
size++;
}
else{
newnode.setNext(node.getNext());
node.setNext(newnode);
}

}
}


测试结果:thy r stdnts
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: