290. Word Pattern

题目描述:

Given a pattern and a string str, find if str follows the same pattern.

 

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

 

Example 1:

 

Input: pattern = "abba", str = "dog cat cat dog"

Output: true

Example 2:

 

Input:pattern = "abba", str = "dog cat cat fish"

Output: false

Example 3:

 

Input: pattern = "aaaa", str = "dog cat cat dog"

Output: false

Example 4:

 

Input: pattern = "abba", str = "dog dog dog dog"

Output: false

Notes:

You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

首先做的一个单个字符串的:

package wp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class wp {
	public static void main(String[] args) {
		//pattern
		System.out.println("please input pattern:");
		Scanner sc=new Scanner(System.in);
		String pattern=sc.nextLine();
		//word
		System.out.println("please input a string word:");
		ArrayList<String> word_str=new ArrayList<>();
		while (true) {
			String word_input=sc.nextLine();
			if (word_input.equals("exit")) {
				break;
			}
			word_str.add(word_input);
		}
		//sign state
		//word_flag
		int[] word_flag=new int[word_str.size()];
		for (int i = 0; i < word_flag.length; i++) {
			word_flag[i]=i;
		}
		//pattern_flag
		int[] pattern_flag=new int[pattern.length()];
		for (int i = 0; i < pattern.length(); i++) {
			pattern_flag[i]=i;
		}
		//judge input true or false
		if (word_str.size()!=pattern.length()) {
			System.out.println("input error");
		}else {
		//convert
		char[] pattrens=pattern.toCharArray();	
		//System.out.println(Arrays.toString(pattrens));	
		//pattern_flag	
			for (int i = 0; i < word_str.size(); i++) {
				for (int j = i+1; j < word_str.size(); j++) {
					if (word_str.get(i).equals(word_str.get(j))) {
						word_flag[j]=word_flag[i];
					}
				}
			}
		//pattern_flag		
			for (int i = 0; i < pattrens.length; i++) {
				for (int j = i+1; j < pattrens.length; j++) {
					if (pattrens[i]==pattrens[j]) {
						pattern_flag[j]=pattern_flag[i];
					}
				}
			}	
            //test
			System.out.println(Arrays.toString(word_flag));
			System.out.println(Arrays.toString(pattern_flag));
			//result(output)
			int a=0;
			for (int i = 0; i < pattrens.length; i++) {
				if (!(word_flag[i]==pattern_flag[i])){
					System.out.println("false");
					a=1;
				    break; 
				}
			}
			if (a==0) {
				System.out.println("true");
			}
			    
		}
	}

}

测试一下:

(1)please input pattern:

abba

please input a string word:

dog

cat

cat

dog

exit

[0, 1, 1, 0]

[0, 1, 1, 0]

true

 

(2)please input pattern:

abba

please input a string word:

dog

cat

cat

fish

exit

[0, 1, 1, 3]

[0, 1, 1, 0]

false

 

(3)please input pattern:

aaaa

please input a string word:

dog

cat

cat

dog

exit

[0, 1, 1, 0]

[0, 0, 0, 0]

false

(4)please input pattern:

abba

please input a string word:

dog

dog

dog

dog

exit

[0, 0, 0, 0]

[0, 1, 1, 0]

false

 

题目要求的是输入一个字符串,改几行代码就行:

package wp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class wp {
	public static void main(String[] args) {
		//pattern
		System.out.println("please input pattern:");
		Scanner sc=new Scanner(System.in);
		String pattern=sc.nextLine();
		//word
		System.out.println("please input a string word:");
		String word_input=sc.nextLine();
		String[] word_str=word_input.split(" +");
		
		//sign state
		//word_flag
		int[] word_flag=new int[word_str.length];
		for (int i = 0; i < word_flag.length; i++) {
			word_flag[i]=i;
		}
		//pattern_flag
		int[] pattern_flag=new int[pattern.length()];
		for (int i = 0; i < pattern.length(); i++) {
			pattern_flag[i]=i;
		}
		//judge input true or false
		if (word_str.length!=pattern.length()) {
			System.out.println("input error");
		}else {
		//convert
		char[] pattrens=pattern.toCharArray();	
		//System.out.println(Arrays.toString(pattrens));	
		//pattern_flag	
			for (int i = 0; i < word_str.length; i++) {
				for (int j = i+1; j < word_str.length; j++) {
					if (word_str[i].equals(word_str[j])) {
						word_flag[j]=word_flag[i];
					}
				}
			}
		//pattern_flag		
			for (int i = 0; i < pattrens.length; i++) {
				for (int j = i+1; j < pattrens.length; j++) {
					if (pattrens[i]==pattrens[j]) {
						pattern_flag[j]=pattern_flag[i];
					}
				}
			}	
            //test
			System.out.println(Arrays.toString(word_flag));
			System.out.println(Arrays.toString(pattern_flag));
			//result(output)
			int a=0;
			for (int i = 0; i < pattrens.length; i++) {
				if (!(word_flag[i]==pattern_flag[i])){
					System.out.println("false");
					a=1;
				    break; 
				}
			}
			if (a==0) {
				System.out.println("true");
			}
			    
		}
	}

}

检验一下:

please input pattern:

abb

please input a string word:

dog cat cat

[0, 1, 1]

[0, 1, 1]

true

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值