Advertisement
1988coder

392. Is Subsequence

Dec 29th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. /**
  2.  * Using 2 indexes. This solution is good for cases when everytime string t is
  3.  * different. If T is same and different s strings are passed, check the below
  4.  * binary search function.
  5.  *
  6.  * Time Complexity: O(T)
  7.  *
  8.  * Space Complexity: O(1)
  9.  *
  10.  * T = length of string t
  11.  */
  12. class Solution {
  13.     public boolean isSubsequence(String s, String t) throws IllegalArgumentException {
  14.         if (s == null || t == null) {
  15.             throw new IllegalArgumentException("Input string is null");
  16.         }
  17.         if (s.length() == 0) {
  18.             return true;
  19.         }
  20.         if (s.length() > t.length()) {
  21.             return false;
  22.         }
  23.  
  24.         int indexS = 0;
  25.         for (int indexT = 0; indexT < t.length(); indexT++) {
  26.             if (s.charAt(indexS) == t.charAt(indexT)) {
  27.                 indexS++;
  28.             }
  29.             if (indexS == s.length()) {
  30.                 return true;
  31.             }
  32.         }
  33.  
  34.         return false;
  35.     }
  36. }
  37.  
  38. /**
  39.  * Preprocess T and use Binary Search to check for isSubsequence.
  40.  *
  41.  * Preporcessing Time Complexity: O(T)
  42.  *
  43.  * Time Complexity of isSubsequence: O(S log(T))
  44.  *
  45.  * Space Complexity: O(T)
  46.  *
  47.  * T = length of string t
  48.  */
  49. class Solution {
  50.     public boolean isSubsequence(String s, String t) throws IllegalArgumentException {
  51.         if (s == null || t == null) {
  52.             throw new IllegalArgumentException("Input string is null");
  53.         }
  54.         if (s.length() == 0) {
  55.             return true;
  56.         }
  57.         if (s.length() > t.length()) {
  58.             return false;
  59.         }
  60.  
  61.         Map<Character, List<Integer>> charMapT = processStr(t);
  62.  
  63.         int prevIdx = -1;
  64.  
  65.         for (char c : s.toCharArray()) {
  66.             if (!charMapT.containsKey(c)) {
  67.                 return false;
  68.             }
  69.             int nextIdx = binarySearchInList(charMapT.get(c), prevIdx);
  70.             if (nextIdx == -1) {
  71.                 return false;
  72.             }
  73.             prevIdx = nextIdx;
  74.         }
  75.         return true;
  76.     }
  77.  
  78.     private Map<Character, List<Integer>> processStr(String s) {
  79.         Map<Character, List<Integer>> map = new HashMap();
  80.         for (int i = 0; i < s.length(); i++) {
  81.             char c = s.charAt(i);
  82.             if (!map.containsKey(c)) {
  83.                 map.put(c, new ArrayList<Integer>());
  84.             }
  85.             map.get(c).add(i);
  86.         }
  87.         return map;
  88.     }
  89.  
  90.     private int binarySearchInList(List<Integer> list, int prev) {
  91.         int start = 0;
  92.         int end = list.size() - 1;
  93.         while (start < end) {
  94.             int mid = start + (end - start) / 2;
  95.             if (list.get(mid) <= prev) {
  96.                 start = mid + 1;
  97.             } else {
  98.                 end = mid;
  99.             }
  100.         }
  101.         return list.get(start) > prev ? list.get(start) : -1;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement