Character (not a string) Inclusion Problem

Source: Internet
Author: User


Problem description:

Assume that there is a character string consisting of letters. Assume that there is another character string, and the number of letters in the string is relatively small. In terms of algorithms, how can we quickly find all the letters in a small string in a large string? For example, if it is the following two strings: String 1: abcdefghlmnopqrs string 2: dcgsrqpom, the answer is true, and all the letters in string2 also have string1. For the following two strings:
String 1: abcdefghlmnopqrs string 2: dcgsrqpoz

The answer is false because the Z letter in the second string is not in the first string.

Related question: the character that appears once in the first value of the string.

Method 1: Hash

Maps each character in string 1 to an array. The subscript of the array is after the mandatory conversion of the char type to the int type.

Value size. The value of the array element is the number of times that the character appears in the string. Take the characters in string 2 one by one

Compare. The time complexity is O (m + n), and the space complexity is O (1) (fixed to 256 );

The hash algorithm is hash (c) = (INT) C.

The Code is as follows:

public class ContainChar {public char[] c1;public char[] c2;public int[] h = new int[256];public ContainChar(String s1,String s2){this.c1 = s1.toCharArray();this.c2 = s2.toCharArray();}public int hash(char c){return (int)c;}public boolean isContain(){for(int i = 0; i < c1.length; i++){h[hash(c1[i])]++;}for(int i = 0; i < c2.length ;i++){if(hash(c2[i]) == 0){return false;}}return true;}public static void main(String[] args){String s1 = "abcdefghijk";String s2 = "dhk";ContainChar c = new ContainChar(s1, s2);System.out.println(c.isContain());}}

Output is true.

If the problem is that the first character only appears once, after ing the string, after traversing the string at a time, find the character that appears only once and return it.

Similarly, you can use this idea to locate the problem only twice, 3, and so on.

String is most suitable for sorting by count, because the maximum character size is very small (256, although char is 2 bytes, but the ASCII code is located in the first 256 ).


Method 2: bitmap

Bitmap is often used for large input scales. It gives you 4 billion input records and a record asking if you are in the input. Bitmap or bloom filter is used at this time.

All are suitable. Principle: If a byte 0 is 00000000, input 4 to 00001000. Enter a value of 7 to 00001001. It means

Use a byte to represent 0 ~ Number of 7. Simple Example:

Public class bitmap {public byte [] B; // input array and maximum value of array public Bitmap (INT [] A, int max) {B = new byte [(max % 8) + 1]; for (INT I = 0; I <. length; I ++) {Add (A [I]) ;}// place the given value in bitmap public void add (INT value) {int K = value/8; // locate the value in the array, where int mod = Value % 8; // locate the position where 1 byte num = 1; B [k] = (byte) (B [k] | (Num <(7-mod);} public Boolean iscontain (INT value) {// determine whether the given number is int K = value/8 in bitmap; // locate the value in the array at int mod = Value % 8; // locate the location where 1 byte num = 1; if (B [k] & (Num <(7-mod) = 0) {return false;} elsereturn true;} public static void main (string [] ARGs) {// the size of 2byte represents 10 int (40 byte) int [] A = {1, 2, 4, 5, 6, 7, 8, 9, 10}; bitmap B = new Bitmap (A, 10); system. out. println (B. iscontain (8) + "" + B. iscontain (11 ));}}

The output is true and false.

Similarly, load the first string into bitmap and compare it in sequence.

(It seems that bitmap is a little complicated... But it can still solve the problem)

For the first character that appears only once, it is more complicated. We use 2 bits to represent a number, and 00 00 01 indicates that 4 appears once, 00 00 00 10 indicates that 4 appears at least twice .......


Method 3: Find 256 prime numbers, which correspond to each character. Multiply string 1. In turn, check whether the product of string 1 can divide the prime numbers corresponding to all characters of string 2.

Amazing practice!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.