Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int shortestWay(String source, String target) {
- int n = source.length();
- int m = target.length();
- boolean validLetter[] = new boolean[26];
- // record exist letter in s
- for(int i = 0; i< n; i++){
- validLetter[source.charAt(i) - 'a'] = true;
- }
- // i and j are indexes of source and target
- int i = 0;
- int j = 0;
- int count = 0;
- while(j < m){
- //
- if(!validLetter[target.charAt(j) - 'a']){
- return -1;
- }
- if(source.charAt(i) == target.charAt(j)){
- j++;
- }
- i++;
- if(i == n){
- i = 0;
- count++;
- }
- }
- if(i != 0) count++;
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement