Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. /*
  2. Runtime: 2 ms, faster than 83.45% of Java online submissions for Letter Case Permutation.
  3. Memory Usage: 37.9 MB, less than 100.00% of Java online submissions for Letter Case Permutation.
  4. */
  5.  
  6. class Solution {
  7.     List<String> result;
  8.     public List<String> letterCasePermutation(String S) {
  9.         result = new ArrayList<String>();
  10.         go(S.toCharArray(), 0);
  11.         return result;
  12.     }
  13.    
  14.     void go(char[] s, int idx) {
  15.         if (idx >= s.length) {
  16.             result.add(new String(s));
  17.             return;
  18.         }
  19.        
  20.         int nextId = idx + 1;
  21.         // while (nextId < s.length && !Character.isLetter(s[nextId])) nextId++;
  22.         if (Character.isLetter(s[idx])) {
  23.             s[idx] = flip(s[idx]);
  24.             go(s, nextId);
  25.             s[idx] = flip(s[idx]);            
  26.         }
  27.         go (s, nextId);
  28.     }
  29.    
  30.     char flip(char c) {
  31.         if (Character.isUpperCase(c)) return Character.toLowerCase(c);
  32.         return Character.toUpperCase(c);
  33.      }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement