Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. class Solution {
  2. public int shortestWay(String source, String target) {
  3. int n = source.length();
  4. int m = target.length();
  5. boolean validLetter[] = new boolean[26];
  6. // record exist letter in s
  7. for(int i = 0; i< n; i++){
  8. validLetter[source.charAt(i) - 'a'] = true;
  9. }
  10. // i and j are indexes of source and target
  11. int i = 0;
  12. int j = 0;
  13. int count = 0;
  14. while(j < m){
  15. //
  16. if(!validLetter[target.charAt(j) - 'a']){
  17. return -1;
  18. }
  19. if(source.charAt(i) == target.charAt(j)){
  20. j++;
  21. }
  22. i++;
  23. if(i == n){
  24. i = 0;
  25. count++;
  26. }
  27. }
  28. if(i != 0) count++;
  29. return count;
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement