`
leonzhx
  • 浏览: 797405 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Question 2. String Containing Problem

阅读更多

Question:  there are two string A & B which only contain upper case English alphabets. B has smaller length than A. How to quickly decide whether all the characters in B are contained in A. For exmaple,

String A : ABCDEFGHLMNOPQRS

String B : DCGSRQPO

Your method take A and B as input and should return true because All characters in B appeared in A too.

String A : ABCDEFGHLMNOPQRS

String B : DCGSRQPZ

Your method take A and B as input and should return false because character 'Z' appeared in B but in A.

 

My thought: It's a gernal question like checking whether a word spelling is correct. We can just setup a HashSet of all the characters appeared in A and then go through characters in B to check whether they are in the HashSet. However since the characters are limited to English alphabets, we can a boolean array of length 26 or even use the lowest 26 bits of an Integer to record the appearance of each alphabet

Although boolean array may waste some space ( depends on JVM implementation of boolean type ) , but I think array access should be faster than bit operation

 

Java Implementation:

public class StringContaining {

	public static void main(String[] args) {
		BooleanArray set1 = new BooleanArray();
		BitsArray set2 = new BitsArray();
		
		assert contains("ABCDEFGHLMNOPQRS" , "DCGSRQPO" , set1);
		assert contains("ABCDEFGHLMNOPQRS" , "DCGSRQPO" , set2);
		
		assert !contains("ABCDEFGHLMNOPQRS" , "DCGSRQPZ" , set1);
		assert !contains("ABCDEFGHLMNOPQRS" , "DCGSRQPZ" , set2);

	}
	
	
	public static boolean contains (String sa, String sb , CharHashSet set) {
		
	    int i = 0;
	    int size = sa.length();
	    for (; i < size; i ++ ) {
	    	set.add(sa.charAt(i));
	    }
	    
	    for ( i = 0 , size = sb.length() ; i < size ; i ++ ) {
	    	if (! set.contains(sb.charAt(i))) return false;
	    }
		
	    return true;
		
	}
	
	public interface CharHashSet {
		public void add(char c);
		
		public boolean contains ( char c);
	}
	
	public static class BooleanArray implements CharHashSet{

		private boolean[] exists = new boolean[26];
		
		@Override
		public void add(char c) {
			exists[c - 'A'] = true;
		}

		@Override
		public boolean contains(char c) {
			return exists[c-'A'];
		}
	}
	
	public static class BitsArray implements CharHashSet{
         
		private int exists;
		
		@Override
		public void add(char c) {
			exists |= 1 << (c - 'A');
			
		}

		@Override
		public boolean contains(char c) {
			
			return ( exists & (1 << (c - 'A')) ) > 0;
		}
		
	}
	

}

 

0
2
分享到:
评论

相关推荐

    计算机网络第六版答案

    Computer Networking: A Top-Down Approach, 6th Edition Solutions to Review Questions and Problems Version Date: May 2012 ...This document contains the solutions to review questions ...Problem 1 There...

    SQLMemTable for Delphi / C++ Builder

    Also, if you are replacing an existing version of SQLMemTable, please remove all files and the package of the prior version before running the new setup program.2) Unpack zip archive containing ...

    visual assist v 10.4.1632 with crack

    (case=9976) 6946 VA Outline shows correct hierarchy for C++ classes containing friend methods without friend class declaration. (case=10740) 7072 .Net symbols are excluded from suggestion ...

    occam一维反演

    c DATIME(DATETM), RETURNS AN 80 CHARACTER STRING WITH CURRENT DATE AND TIME. CAN C BE A DUMMY ROUTINE IF SYSTEM PROGRAMMING IS TOO MUCH. C INPUTD(FILENAME), TAKES THE NAMED FILE AND USES IT TO GET ...

    Turbo C++ 3.0[DISK]

    a free IntroPak containing a $15 credit toward your first month's on-line charges. 2. Check with your local software dealer or users' group. 3. Borland's TECHFAX service. Call (800) 822-4269 for...

    Turbo C++ 3.00[DISK]

    a free IntroPak containing a $15 credit toward your first month's on-line charges. 2. Check with your local software dealer or users' group. 3. Borland's TECHFAX service. Call (800) 822-4269 for...

    微软内部资料-SQL性能优化3

    Intent lock is the answer to this problem. Intent Lock Intent Lock is the term used to mean placing a marker in a higher-level lock queue. The type of intent lock can also be called the multigranular...

    UE(官方下载)

    The List Lines Containing String option in Find The lists lines option can be a handy tool when searching because it presents all occurrences of the find string in a floating dialog box. You can use ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    There are some common exceptions, such as unittests and small .cc files containing just a main() function. Correct use of header files can make a huge difference to the readability, size and ...

Global site tag (gtag.js) - Google Analytics