Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s, t, k): n, m = len(s), len(t) if n != m: return False cnt = [0] * 26 for i in range(n): if s[i] == t[i]: continue diff = (ord(t[i]) - ord(s[i])) % 26 if cnt[diff] * 26 + diff > k: return False cnt[diff] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False shifts = [0 for x in range(1, 27)] for i in range(len(s)): if t[i] == s[i]: continue diff = (ord(t[i]) - ord(s[i])) % 26 if ((shifts[diff]) * 26 + diff) > k: return False shifts[diff] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False dd = collections.defaultdict(int) for i,v in zip(s,t): diff = (ord(v) - ord(i)) % 26 if diff > 0 and diff + dd[diff] * 26 > k: return False dd[diff] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
import collections class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: # check lengths n1 = len(s) n2 = len(t) if n1 != n2: return False n = n1 # find diffArray diffArray = [(ord(t[i])-ord(s[i]))%26 for i in range(n)] # frequency of difference cda = collections.Counter(diffArray) # delete 0 del cda[0] minK = 0 if len(cda) > 0 : key = max([(i[1], i[0])for i in list(cda.items())]) #print(key) minK = (key[0] - 1) *26 + key[1] return k >= minK
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False cnt = [0] * 26 for cs, ct in zip(s, t): diff = (ord(ct) - ord(cs)) % 26 if diff > 0 and cnt[diff] * 26 + diff > k: return False cnt[diff] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False moves = collections.Counter() for si, ti in zip(s, t): shifts = (ord(ti) - ord(si)) % 26 moves[shifts] += 1 for i in range(1, 26): total_moves = moves[i] if i + 26*(total_moves-1) > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: jumps=list(range(26)) if len(s)!=len(t): return False for i in range(len(s)): if s[i]==t[i]: continue jump=(ord(t[i])-ord(s[i])+26)%26 if jumps[jump]>k: return False jumps[jump]+=26 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
from collections import Counter class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False cnt = [0] * 26 for cs, ct in zip(s, t): diff = (ord(ct) - ord(cs)) % 26 if diff > 0 and cnt[diff] * 26 + diff > k: return False cnt[diff] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: n1, n2 = len(s), len(t) if n1 != n2: return False if k == 0: return s == t # record how many letters need to be changed, autobump step if step is found steps = collections.defaultdict(int) for sc, tc in zip(s, t): if sc != tc: idx_t = ord(tc) idx_s = ord(sc) if idx_s < idx_t: step = idx_t - idx_s else: step = idx_t - idx_s + 26 if step > k: return False steps[step] += 1 for step in steps: if (steps[step] - 1) * 26 + step > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
def dist(s_c, t_c): if s_c == t_c: return 0 elif s_c < t_c: return ord(t_c) - ord(s_c) else: # s_c > t_c return ord('z') - ord(s_c) + ord(t_c) - ord('a') + 1 class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: distances = [0] * 26 if len(s) != len(t): return False for s_c, t_c in zip(s, t): d = dist(s_c, t_c) if d != 0: distances[d] += 1 for i, d in enumerate(distances): if d > 0: if (d - 1) * 26 + i > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: o = {} if len(s) != len(t):return False for i in range(len(s)): l = (ord(t[i]) - ord(s[i]))%26 if l == 0:continue if l > k :return False if l not in o : o[l] = l else: last = o[l] if last + 26 > k: return False o[l] = 26 + last return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False distance = Counter((ord(b) - ord(a)) % 26 for a, b in zip(s, t) if a != b) maxf = [-1, -1] for c, f in distance.items(): if f > maxf[1] or f == maxf[1] and c > maxf[0]: maxf[0] = c maxf[1] = f return not distance or ((maxf[1] - 1) * 26 + maxf[0]) <= k
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False cnts = [0]*26 for s_char, t_char in zip(s, t): if s_char == t_char: continue elif s_char > t_char: moves_req = 26 - (ord(s_char)-ord(t_char)) else: moves_req = ord(t_char) - ord(s_char) cnts[moves_req] += 1 if moves_req > k or cnts[moves_req] > (k-moves_req)//26+1: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if(len(s) != len(t)): return False idx_dict = [0] * 26 # for i in range(1, 26): # idx_dict[i] = 0 for idx, sc in enumerate(s): tc = t[idx] if(sc == tc): # idx_list.append(0) continue sasc = ord(sc) tasc = ord(tc) if(sasc < tasc): movei = tasc - sasc else: movei = 26 - sasc + tasc # while(movei in idx_set): # movei = movei + 26 origini = movei movei = movei + 26 * idx_dict[movei] idx_dict[origini] += 1 if(movei > k): return False # idx_list.append(movei) # idx_set.add(movei) #print(idx_list) return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
def no_of_shifts_away(a, b): return (ord(b) - ord(a)) % 26 class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: in1_len = len(s) in2_len = len(t) max_duplicates_in_shift = [] for i in range(26): max_duplicates_in_shift.append(max(0, 1 + ((k - i) // 26))) if in1_len != in2_len: return False for i in range(in1_len): shifts_required = no_of_shifts_away(s[i], t[i]) if shifts_required > k: return False if shifts_required != 0: max_duplicates_in_shift[shifts_required] -= 1 if max_duplicates_in_shift[shifts_required] < 0: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: change = [] if len(s)!=len(t): return False for a,b in zip(s,t): if a!=b: change.append(ord(b)-ord(a)) for index,number in enumerate(change): if number < 0: change[index]+=26 count = [0]*26 for index,number in enumerate(count): count[index]+=int(k/26) remaining = k%26 i=1 for j in range(remaining): count[i]+=1 i+=1 for number in change: if count[number]==0: return False else: count[number]-=1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: a = [k // 26 for _ in range(27)] for i in range(k % 26 + 1): a[i] += 1 ns, nt = len(s), len(t) if ns != nt: return False for i in range(ns): if s[i] == t[i]: continue else: d = ord(t[i]) - ord(s[i]) if t[i] > s[i] else ord(t[i]) + 26 - ord(s[i]) if a[d] <= 0: return False a[d] -= 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: moves = defaultdict(lambda: 0) K_div = k // 26 K_mod = k % 26 if len(s) != len(t): return False for cs, ct in zip(s, t): ordc = ord(cs) ordt = ord(ct) move = ordt - ordc if move < 0: move += 26 if move == 0: continue numMoves = K_div + (1 if move <= K_mod else 0) if moves[move] == numMoves: return False moves[move] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False l = [0] * 26 for i in range(len(s)): if s[i] == t[i]: continue x = (ord(t[i]) - ord(s[i]) + 26) % 26 if l[x] * 26 + x > k: return False l[x] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if(s==t): return True if(len(s)!=len(t)): return False has={} for i in range(1,27): num=26+i if(i<=k): has[i]=1 x=(k-i)//26 has[i]+=x n=len(s) print(has) count=0 for i in range(n): if(s[i]==t[i]): count+=1 continue diff=(ord(t[i])-ord(s[i]))%26 if(diff in has): count+=1 has[diff]-=1 if(has[diff]==0): del has[diff] else: return False # print(has2) if(count==n): return True return False
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: n = len(s) m = len(t) if m!=n: return False if s==t: return True shift = {} shift[0] = 0 for i in range(n): move = (ord(t[i])-ord(s[i]))%26 if move>0: if move in shift: new_move = 26*shift[move] + move shift[move] = shift[move] + 1 move = new_move else: shift[move] = 1 if move>k: return False else: shift[0] = shift[0] + 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
import collections class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: # check lengths n1 = len(s) n2 = len(t) if n1 != n2: return False n = n1 # find diffArray diffArray = [(ord(t[i])-ord(s[i]))%26 for i in range(n)] # frequency of difference cda = collections.Counter(diffArray) print(cda) # delete 0 del cda[0] elements = sorted(list(cda.items()), key=lambda x: (x[1], x[0]), reverse=True) minK = 0 try: key = elements[0] minK = (key[1] - 1) *26 + key[0] except IndexError: pass print(minK) return k >= minK
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return 0 aux = dict() for i in range(len(s)): if s[i] != t[i]: val1, val2 = ord(s[i]), ord(t[i]) if val1 < val2: if val2 - val1 in aux: aux[val2 - val1] += 1 else: aux[val2 - val1] = 1 else: if 26 - val1 + val2 in aux: aux[26 - val1 + val2] += 1 else: aux[26 - val1 + val2] = 1 ans = 0 for elem in aux: ans = max(ans, 26 * (aux[elem]-1) + elem) return ans <= k
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False used = {} for i in range(len(s)): diff = ord(s[i]) - ord(t[i]) if diff == 0: continue i = (26-diff) if diff > 0 else abs(diff) if i > k: return False if i not in used: used[i] = 1 else: move = used[i] * 26 + i if move > k: return False else: used[i] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: # a, b if len(s) != len(t): return False largest = [0]*26 for i in range(len(s)): diff = ord(t[i]) - ord(s[i]) if diff == 0: continue elif diff < 0: diff += 26 if largest[diff-1] == 0: largest[diff-1] = diff else: largest[diff-1] += 26 if largest[diff-1] > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False d = defaultdict(int) for sc, tc in zip(s,t): diff = ( ord(tc) - ord(sc) ) % 26 if diff == 0: continue if diff > k: return False d[diff] += 1 if ((d[diff] - 1) * 26) + diff > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False maps = {} # or maybe use array for i in range(len(s)): if s[i] != t[i]: diff = (ord(t[i]) - ord(s[i]) ) % 26 if diff not in list(maps.keys()): maps[diff] = 1 else: maps[diff] += 1 if ((maps[diff] - 1) * 26 + diff) > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False alreadySeen = {} for i in range(len(s)): if s[i] != t[i]: difference = (ord(t[i]) - ord(s[i])) % 26 if difference > k: return False if difference not in alreadySeen: alreadySeen[difference] = 1 else: newDifference = difference + alreadySeen[difference] * 26 if newDifference > k: return False else: alreadySeen[difference] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def dist(self, a,b): return (ord(b)-ord(a)) % 26 def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False seen = {} for x, y in zip(s, t): if x == y: continue d = self.dist(x,y) if d > k: return False if d not in seen: seen[d] = d else: last = seen[d] if last + 26 > k: return False seen[d] = last + 26 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
from collections import Counter class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False ans = Counter() for i in range(len(s)): key = (ord(t[i]) - ord(s[i])) % 26 if key != 0 and ans[key] * 26 + key > k: return False ans[key] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False cnt = collections.defaultdict(int) for i in range(len(s)): if s[i] == t[i]: continue shift = 0 if ord(t[i]) < ord(s[i]): shift = ord(t[i]) - ord(s[i]) + 26 else: shift = ord(t[i]) - ord(s[i]) cnt[shift] += 1 for r in cnt: # We have cnt[r] letters in s that will need to be shifted r letters to line up with t. # We then simply need to check that k is large enough so that there are enough instances # of i, 1 <= i <= k, such that i % 26 == r. # How do we check? Well if x * 26 + r <= k, then we have (0*26 + r) % 26 == r, # (1*26 + r) % 26 == r, (2*26 + r) % 26 == r, ..., (x*26 + r) % 26 == r, i.e there are x + 1 # instances of i such that i % 26 == r. # Given cnt[r] letters that need to be shifted, we only need to make sure that: # (cnt[r]-1)*26 +r <= k # if (cnt[r] - 1) * 26 + r > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
def no_of_shifts_away(a, b): return (ord(b) - ord(a)) % 26 class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: in1_len = len(s) freq = {} in2_len = len(t) max_duplicates_in_shift = [] for i in range(26): max_duplicates_in_shift.append(max(0, 1 + ((k - i) // 26))) if in1_len != in2_len: return False for i in range(in1_len): shifts_required = no_of_shifts_away(s[i], t[i]) if shifts_required > k: return False if shifts_required != 0: if shifts_required in freq: freq[shifts_required] += 1 if freq[shifts_required] > max_duplicates_in_shift[shifts_required]: return False else: freq[shifts_required] = 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False d = {} for i in range(len(s)): if s[i] == t[i]: continue diff = (ord(t[i]) - ord(s[i])) % 26 if diff not in d: d[diff] = 1 else: d[diff] += 1 if (26 * (d[diff]-1)) + diff > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def getMoves(self, cFrom, cTo): return (ord(cTo) - ord(cFrom)) % 26 def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False usedDict = {} for cFrom, cTo in zip(s, t): if cFrom == cTo: continue moves = self.getMoves(cFrom, cTo) if moves > k: return False if moves not in usedDict: usedDict[moves] = 1 else: if moves + (26 * usedDict[moves]) <= k: usedDict[moves] += 1 else: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False isUsed = dict() for i in range(len(s)): ch1, ch2 = ord(s[i]), ord(t[i]) if ch1 < ch2: shift = ch2-ch1 elif ch1 > ch2: shift = 26-(ch1-ch2) else: continue initShift = shift while 1 <= shift <= k: if shift not in isUsed: isUsed[initShift] = shift break shift = isUsed[initShift]+26 else: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False #edge case visited = {} for ss, tt in zip(s, t): if ss != tt: d = (ord(tt) - ord(ss)) % 26 #distance if d not in visited: visited[d] = d else: visited[d] += 26 if visited[d] > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: from collections import defaultdict if len(s) != len(t): return False i = 0 j = defaultdict(int) print((len(s))) while i < len(s): mr = (ord(t[i]) - ord(s[i]))%26 if mr == 0: pass else: if mr + j.get(mr,0) * 26 > k: return False j[mr] += 1 i += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s)!=len(t): return False hash_map = {} for i in range(len(s)): if s[i]!=t[i]: diff = -1*(ord(s[i])-ord(t[i])) if diff<0: diff = 26+diff if diff in hash_map: hash_map[diff]+=1 diff += (hash_map[diff]-1)*26 else: hash_map[diff] = 1 if diff>k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False available, maxi = [0 if k < i else 1 + (k - i) // 26 for i in range(26)], 0 for a, b in zip(s, t): if a == b: continue require = (26 + (ord(b) - ord('a')) - (ord(a) - ord('a'))) % 26 if not available[require]: return False available[require] -= 1 maxi = max(maxi, require) return maxi <= k
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False counts = [0] * 26 for si, ti in zip(s, t): difference = ord(ti) - ord(si) if difference < 0: difference += 26 counts[difference] += 1 for i, count in enumerate(counts[1:], 1): maxConvert = i + 26 * (counts[i] - 1) if maxConvert > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False n = len(s) occ = {} for i in range(n): l = (ord(t[i]) - ord(s[i]))%26 if not l: continue try: if occ[l]: occ[l] += 1 l = (occ[l]-1)*26 + l if l > k: return False except: if l > k: return False occ[l] = 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
from collections import defaultdict class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False cnt = defaultdict(int) res = 0 for i in range(len(s)): j = (ord(t[i]) - ord(s[i]) + 26) % 26 if j: res = max(res, 26 * cnt[j] + j) cnt[j] += 1 return res <= k
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False shift_needed = collections.defaultdict(int) for sc, tc in zip(s, t): if sc > tc: shift_needed[ord('z')-ord(sc)+ord(tc)-ord('a')+1] += 1 elif sc < tc: shift_needed[ord(tc)-ord(sc)] += 1 min_move_needed = 0 for shift, v in list(shift_needed.items()): min_move_needed = max(min_move_needed, 26*(v-1)+shift) #print(shift_needed, min_move_needed) return min_move_needed <= k
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: d = {} if(len(s)!= len(t)): return False for i in range(len(s)): if(s[i]!= t[i]): if(ord(s[i])<ord(t[i])): val = ord(t[i])-ord(s[i]) else: val = (ord('z')-ord(s[i]))+(ord(t[i])-ord('a'))+1 if(val in d): d[val]+=1 elif(val not in d): d[val] = 1 for x in d: if(((d[x]-1)*26+x)>k): return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False maps = {} for i in range(len(s)): if s[i] != t[i]: diff = (ord(t[i]) - ord(s[i]) ) % 26 if diff not in list(maps.keys()): maps[diff] = 1 else: maps[diff] += 1 if ((maps[diff] - 1) * 26 + diff) > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t) or len(s) == 0: return False dists = [(26 + ord(b) - ord(a)) % 26 for a, b in zip(s, t)] counter = Counter(dists) print((dists, counter)) for i in range(1, 26): if counter[i] > (k - i) // 26 + 1: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if(len(s) != len(t)): return False idx_set = {0} idx_list = [] idx_dict = {} for i in range(1, 26): idx_dict[i] = 0 for idx, sc in enumerate(s): tc = t[idx] if(sc == tc): # idx_list.append(0) continue sasc = ord(sc) tasc = ord(tc) if(sasc < tasc): movei = tasc - sasc else: movei = 26 - sasc + tasc # while(movei in idx_set): # movei = movei + 26 origini = movei movei = movei + 26 * idx_dict[movei] idx_dict[origini] += 1 if(movei > k): return False # idx_list.append(movei) # idx_set.add(movei) #print(idx_list) return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False def aIntoB(a,b)->int: return (ord(b)-ord(a)) % 26 moves = [k//26]*26 for i in range((k % 26) + 1): moves[i] += 1 for a, b in zip(s, t): if a == b: continue moves[aIntoB(a,b)] -= 1 if moves[aIntoB(a,b)] < 0: return False return True # 3 -> # 1 1 1 0 0 0 ... # 29 -> # 2 2 2 1 1 1 ... # 27 -> # 2 1 1 1 1 1 ...
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False diff = [(ord(c2)-ord(c1)) % 26 for c1, c2 in zip(s, t)] ctr = sorted((v, k) for k, v in list(collections.Counter(diff).items()) if k) return not ctr or (ctr[-1][0]-1) * 26 + ctr[-1][1] <= k
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False count = {} for x,y in zip(s,t): change = 0 if ord(y) - ord(x) > 0: change = ord(y) - ord(x) elif ord(y) - ord(x) < 0: change = (ord(y) - ord(x)) + 26 if change == 0: continue if change not in count: count[change] = 1 else: count[change] += 1 if (count[change] - 1) * 26 + change > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, S: str, T: str, k: int) -> bool: if len(S) != len(T): return False dic = {} for s, t in zip(S, T): shift = (ord(t) - ord(s))%26 if not shift: continue if shift not in dic:dic[shift] = 0 dic[shift] += 1 if shift + 26*(dic[shift] - 1) > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: # convert s to t in k moves or less # so the ith move is 1 <= i <= k # pick a character at j (1-indexed) in the string and shift it i times # or you can do nothing in the move. # 'shifting' the character means to replace it to the next letter in the alphabet alpha = 'abcdefghijklmnopqrstuvwxyz' # any index j can be picked at most ONCE # for each x [1..k] you have an opportunity to shift a character in s, i times. # the answer is false if there is a character that needs more shifts than we are able to supply # can create a list of deltas, sorted descending, and see if we have enough space to work with deltas = [] if len(s) != len(t): return False for i,c in enumerate(s): t_index = ord(t[i]) - ord('a') s_index = ord(s[i]) - ord('a') deltas.append((t_index - s_index)%26) m = collections.defaultdict(int) while k%26 != 0: m[k%26] += 1 k -= 1 for i in range(1,26): m[i] += k//26 #print(sorted(deltas, reverse=True)) for d in sorted(deltas, reverse=True): if not d: continue if not m[d]: return False m[d] -= 1 # take the highest available k that will satisfy that delta # ie, take the highest number x such that x%26 == d %26 # or you can have a count of all the numbers that land in each class return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False valid = {} moves = [] for a,b in zip(s,t): if a != b: diff = ord(b) - ord(a) if diff < 0: diff += 26 if diff in valid: valid[diff].append(valid[diff][-1]+26) else: valid.setdefault(diff,[diff]) moves.append(diff) for d in moves: num = valid[d] if num[-1] > k: return False else: num.pop() return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: n = len(s) m = len(t) if(n!=m): return False else: diff = {} d = {} for i in range(n): if(s[i]!=t[i]): if(t[i]>s[i]): value = ord(t[i])-ord(s[i]) else: value = (26-ord(s[i]))+ord(t[i]) if value not in diff: start = value else: start = diff[value]+26 flag = 0 while(start<=k): if start not in d: d[start] = 1 diff[value] = start flag = 1 break start+=26 if(flag==0): return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s)!=len(t): return False hash={} n=len(s) for i in range(n): x=ord(t[i])-ord(s[i]) if x==0: continue if x>0: if x in hash: hash[x]+=[x+26*(len(hash[x]))] else: hash[x]=[x] elif x<0: if 26+x in hash: hash[26+x]+=[26+x+26*(len(hash[26+x]))] else: hash[26+x]=[26+x] for a in hash: if a>k: return False for i in hash[a]: if i>k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False C = Counter() seen = set() for i in range(len(s)): if s[i] != t[i]: dist = (ord(t[i]) + 26 - ord(s[i])) % 26 if dist not in C.keys(): C[dist] = 0 else: C[dist] += 1 dist += 26 * (C[dist]) if dist > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False n = len(s) g = [0 for i in range(26)] for i in range(n): g[(ord(t[i]) - ord(s[i])) % 26] += 1 for i in range(1, 26): index = i + (g[i]-1) * 26 if index > k: return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: def getScore(sc, tc): if ord(sc) <= ord(tc): return ord(tc) - ord(sc) return ord('z') - ord(sc) + 1 + ord(tc) - ord('a') if len(s) != len(t): return False score_ref = dict() for idx in range(len(s)): score = getScore(s[idx], t[idx]) if score == 0: continue score_ref[score] = score if score_ref.get(score, 0) == 0 else score_ref[score] + 26 return all([score_ref[score] <= k for score in score_ref])
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: n,m = len(s), len(t) if n != m: return False ls = [ord(j)-ord(i) for i, j in zip(s,t)] for i in range(n): if ls[i] < 0: ls[i] = 26 + ls[i] d = {} ans = 0 # print(ls) for i in ls: if i == 0: continue if i not in d: ans = max(ans,i) d[i] = 1 else: ans = max(26*d[i]+i, ans) d[i] += 1 if ans > k: # print(ans, k, d) return False return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False mp = {} for i in range(len(s)): v = ord(t[i]) - ord(s[i]) if v == 0: continue if v < 0: v += 26 # shift backward if v > k: return False if v in mp: # the highest number high_v = mp[v] + 26 if high_v > k: return False mp[v] = high_v else: mp[v] = v return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False cnt = [0] * 26 for cs, ct in zip(s, t): diff = (ord(ct) - ord(cs)) % 26 if diff > 0 and cnt[diff] * 26 + diff > k: return False cnt[diff] += 1 return True
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: b=0 flag=0 lis=[] if(len(s)==len(t)): for i in range(len(s)): if(s[i]!=t[i]): if(s[i]<t[i]): lis.append(ord(t[i])-ord(s[i])) else: lis.append(26-(ord(s[i])-ord(t[i]))) lis.sort() i=0 c=0 while(i<len(lis)-1): if(lis[i]==lis[i+1]): c=c+1 else: if(lis[i]+(c*26)>k): flag=1 break else: c=0 i=i+1 if(len(lis)>1): if(lis[-1]!=lis[-2]): if(lis[-1]>k): flag=1 if(len(lis)>0): if(lis[i-1]+(c*26)>k): flag=1 if(flag==0): b=1 return(b)
Given two strings s and t, your goal is to convert s into t in k moves or less. During the ith (1 <= i <= k) move you can: Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times. Do nothing. Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times. Remember that any index j can be picked at most once. Return true if it's possible to convert s into t in no more than k moves, otherwise return false.   Example 1: Input: s = "input", t = "ouput", k = 9 Output: true Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'. Example 2: Input: s = "abc", t = "bcd", k = 10 Output: false Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s. Example 3: Input: s = "aab", t = "bbb", k = 27 Output: true Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.   Constraints: 1 <= s.length, t.length <= 10^5 0 <= k <= 10^9 s, t contain only lowercase English letters.
class Solution: def canConvertString(self, s: str, t: str, k: int) -> bool: if len(s) != len(t): return False count = {} total = [0] # print(count) for i in range(len(s)): if ord(s[i]) > ord(t[i]): # print('case {} {}'.format(s[i], t[i])) x = 122 - ord(s[i]) + ord(t[i]) - 96 else: x = ord(t[i]) - ord(s[i]) # print(\"({} {})\".format(x,i)) if x != 0: if x not in count: count[x] = 0 if count[x] * 26 + x: max = count[x] * 26 + x if max > k: return False count[x] += 1 # print(max(total)) return True
Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.   Example 1: Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4 Output: 2 Explanation: The maximum side length of square with sum less than 4 is 2 as shown. Example 2: Input: mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1 Output: 0 Example 3: Input: mat = [[1,1,1,1],[1,0,0,0],[1,0,0,0],[1,0,0,0]], threshold = 6 Output: 3 Example 4: Input: mat = [[18,70],[61,1],[25,85],[14,40],[11,96],[97,96],[63,45]], threshold = 40184 Output: 2   Constraints: 1 <= m, n <= 300 m == mat.length n == mat[i].length 0 <= mat[i][j] <= 10000 0 <= threshold <= 10^5
class Solution: def maxSideLength(self, mat: List[List[int]], threshold: int) -> int: dp = [[0 for _ in range(len(mat[0]) + 1)]for r in range(len(mat) + 1)] for r in range(1, len(mat) + 1): for c in range(1, len(mat[r-1]) + 1): dp[r][c] += mat[r-1][c-1] if not r and not c: continue elif not r: dp[r][c] += dp[r][c-1] continue elif not c: dp[r][c] += dp[r-1][c] continue dp[r][c] += dp[r][c-1] + dp[r-1][c] - dp[r-1][c-1] # print(dp) highest = -1 for r in range(1, len(dp)): r0= r1 = r c0= c1 = 1 while r1 < len(dp) and c1 < len(dp[0]): result = dp[r1][c1] + dp[r0-1][c0-1] - dp[r1][c0-1] - dp[r0-1][c1] # print(f'r0:{r0} r1:{r1} c0:{c0} c1:{c1} result:{result}') if result <= threshold: highest = max(r1-r0, highest) r1 += 1 c1 +=1 else: r1 -=1 c0 +=1 r1 = max(r0+1,r1) c1 = max(c0+1,c1) return highest + 1
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B. Example 1: Input: nums = [1,3,1] k = 1 Output: 0 Explanation: Here are all the pairs: (1,3) -> 2 (1,1) -> 0 (3,1) -> 2 Then the 1st smallest distance pair is (1,1), and its distance is 0. Note: 2 . 0 . 1 .
class Solution: def smallestDistancePair(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ nums.sort() l, r = 0, nums[-1] - nums[0] while l < r: m = l + (r - l) // 2 count = 0 left = 0 for right in range(len(nums)): while nums[right] - nums[left] > m: left += 1 count += (right - left) if count < k : l = m+1 else: r = m return l
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B. Example 1: Input: nums = [1,3,1] k = 1 Output: 0 Explanation: Here are all the pairs: (1,3) -> 2 (1,1) -> 0 (3,1) -> 2 Then the 1st smallest distance pair is (1,1), and its distance is 0. Note: 2 . 0 . 1 .
class Solution(object): def smallestDistancePair(self, nums, k): def possible(guess): #Is there k or more pairs with distance <= guess? count = left = 0 for right, x in enumerate(nums): while x - nums[left] > guess: left += 1 count += right - left return count >= k nums.sort() lo = 0 hi = nums[-1] - nums[0] while lo < hi: mi = (lo + hi) // 2 if possible(mi): hi = mi else: lo = mi + 1 return lo
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: tokens = sorted(tokens) left = 0 right = len(tokens) - 1 points = 0 if len(tokens) == 1: if tokens[0] <= P: return 1 if len(tokens) == 0: return 0 while left < right: if tokens[left] <= P: P -= tokens[left] left += 1 points += 1 elif tokens[left] > P and points > 0: P += tokens[right] points -= 1 right -= 1 elif points == 0 and tokens[left] > P: break if P >= tokens[left]: points += 1 return points
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: # O(nlog(n) + n) = O(n(log(n) + 1)) = O(nlog(n)) time / O(1) space or memory # Where 'n' is the number of tokens passed in the input def bagOfTokensScore(self, tokens: List[int], P: int) -> int: # we wanna maximize the points in this game (by giving the power to get points) # we can give point away to get power tokens.sort() # O(mlog(m)) max_points = 0 points = 0 i = 0 j = len(tokens) - 1 while i <= j: if P >= tokens[i]: points += 1 # get a coin P -= tokens[i] # give up power max_points = max(points, max_points) i += 1 elif points > 0: points -= 1 # give up that coin P += tokens[j] # get power j -= 1 else: return max_points return max_points
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: max_points, points = 0, 0 tokens.sort() i, j = 0, len(tokens) - 1 while i <= j: if P < tokens[i]: if i == j or points == 0: break P += tokens[j] points -= 1 j -= 1 points += 1 P -= tokens[i] i += 1 max_points = max(max_points, points) return max(max_points, points)
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: tokens.sort() if(not tokens or P < tokens[0]): return 0 l, r = 0, len(tokens)-1 points = 0 while(l <= r): if(P >= tokens[l]): points += 1 P -= tokens[l] l += 1 else: if(r-l > 1): points -= 1 P += tokens[r] r -= 1 else: break return points
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: if not tokens: return 0 tokens.sort() point = 0 while tokens: if P < tokens[0]: if point and len(tokens) > 1: P += tokens.pop() point -= 1 else: break else: P -= tokens.pop(0) point += 1 return point
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: # use 100, P = 100, p = 1 # 400 down, P = 500, p = 0 # use 200, P = 300, p = 1 # use 300, P = 0, p = 2 ''' sort tokens play smallest tokens until you can't anymore turn over largest tokens until you can play smaller tokens again ''' tokens.sort() maxPoints = 0 points = 0 left, right = 0, len(tokens) - 1 while left <= right and P >= 0: if P - tokens[left] >= 0: P -= tokens[left] points += 1 maxPoints = max(maxPoints, points) left += 1 else: if points > 0: P += tokens[right] points -= 1 right -= 1 else: break return max(maxPoints, points)
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: tokens.sort() left = 0 right = len(tokens) - 1 ans = 0 curr = 0 while not left > right: if P >= tokens[left]: curr += 1 ans = max(ans, curr) P -= tokens[left] left += 1 elif curr > 0: P += tokens[right] curr -= 1 right -= 1 else: break return ans
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: n = len(tokens) tokens.sort() i = 0 j = n-1 points = 0 result = 0 while i <= j: if tokens[i] <= P: points += 1 result = max(result, points) P -= tokens[i] i += 1 else: if points == 0: break P += tokens[j] points -= 1 j -= 1 return result
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: max_points, points = 0, 0 tokens.sort() i, j = 0, len(tokens) - 1 while i <= j: if P < tokens[i]: if i == j or points == 0: break P += tokens[j] points -= 1 j -= 1 points += 1 P -= tokens[i] i += 1 max_points = max(max_points, points) return max_points
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: startIndex = 0 endIndex = len(tokens) - 1 points = 0 tokens.sort() while(True): print(startIndex, endIndex, P) if(startIndex > endIndex or endIndex < startIndex): return points if(P >= tokens[startIndex]): points += 1 P -= tokens[startIndex] startIndex += 1 else: if(points == 0): return points else: if(startIndex == endIndex): return points else: P += tokens[endIndex] endIndex -= 1 points -= 1
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: tokens.sort() buyer,seller = 0,len(tokens)-1 max_items = current_items = 0 while buyer <= seller: if P >= tokens[buyer]: P -= tokens[buyer] current_items+=1 buyer+=1 else: if current_items ==0: break; else: P += tokens[seller] current_items -=1 seller -=1 max_items = max(max_items,current_items) return max_items
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: if not len(tokens) : return 0 tokens.sort() res = 0 deque = collections.deque(tokens) while( len( deque ) > 1 and ( P >= deque[0] or res ) ): while( deque and P > deque[0] ): res += 1 P -= deque.popleft() if( len( deque ) > 1 and res ): P += deque.pop() res -= 1 if ( deque and P >= deque[0] ): res += 1 return res
You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be used at most once, has a value token[i], and has potentially two ways to use it. If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point. If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point. Return the largest number of points we can have after playing any number of tokens.   Example 1: Input: tokens = [100], P = 50 Output: 0 Example 2: Input: tokens = [100,200], P = 150 Output: 1 Example 3: Input: tokens = [100,200,300,400], P = 200 Output: 2   Note: tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000
class Solution: # all the tokens start in a neutral position. Can either play it face up or face down. def bagOfTokensScore(self, tokens: List[int], P: int) -> int: tokens.sort() # start from front and back. greedily take all the powers from the front and spend score in back to get more to add to the front def twoPointer(tokens, P): i = 0 j = len(tokens)-1 score = 0 maxScore = 0 while i <= j: flag = False while i < len(tokens) and P >= tokens[i]: score += 1 P -= tokens[i] i += 1 flag = True if flag: maxScore = max(score, maxScore) continue if score > 0: score -= 1 P += tokens[j] j -= 1 elif P < tokens[i]: return score return maxScore def helper(tokens, P, score): maxVal = score if score > 0 and len(tokens) > 0: maxVal = max(helper(tokens[:len(tokens)-1], P + tokens[len(tokens)-1], score - 1), maxVal) for i in range(len(tokens)-1, -1, -1): if P >= tokens[i]: maxVal = max(helper(tokens[:i] + tokens[i+1:], P - tokens[i], score + 1), maxVal) return maxVal return twoPointer(tokens, P)
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: if not A: return 0 nums = sorted([num + K for num in set(A)], reverse=True) max_num = nums[0] min_num = nums[-1] changed_max = max_num - 2 * K res = max_num - min_num for i in range(len(nums) - 1): changed = nums[i] - 2 * K max_num = max(nums[i + 1], changed, changed_max) min_num = min(min_num, changed) res = min(res, max_num - min_num) return res
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() minA = A[0] maxA = A[-1] smallest = maxA - minA for i in range(len(A) - 1): a = A[i] b = A[i+1] smallest = min(smallest, max(maxA-K, a+K) - min(minA+K, b-K)) return smallest
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() mi, ma = A[0], A[-1] ans = ma-mi for i in range(len(A)-1): a, b = A[i], A[i+1] ans = min(ans, max(ma-K, a+K) - min(mi+K, b-K)) return ans
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() diff = A[-1] - A[0] for i in range(len(A) - 1): lower = min(A[0] + 2 * K, A[i + 1]) upper = max(A[-1], A[i] + 2 * K) diff = min(diff, abs(upper - lower)) return diff
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: if K == 0: return max(A)-min(A) if len(A) == 1: return 0 if max(A) - min(A) <= K: return max(A) - min(A) else: A = sorted(A) dp = [A[-1] - A[0]] # A = sorted(A) n1, n2 = A[-1] - K, A[0] + K for i in range(len(A)-1): dp += [max(A[i]+K, A[-1]-K) - min(A[0]+K, A[i+1]-K)] return min(dp)
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: if len(A) == 1: return 0 A.sort() i = 0 j = len(A) - 1 min_diff = A[-1] - A[0] bottom = A[0] peak = A[-1] for idx in range(len(A) - 1): current, _next = A[idx], A[idx + 1] bottom = min(A[0] + K, _next - K) peak = max(A[-1] - K, current + K) min_diff = min(min_diff, peak - bottom) return min_diff
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() res = A[-1] - A[0] for i in range(len(A)-1): res = min(res, max(A[i]+K, A[-1] - K) - min(A[0] + K, A[i+1] - K)) return res
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() mini = A[0] maks = A[-1] res = maks - mini for i in range(len(A)-1): res = min(res, max(maks-K, A[i]+K) - min(mini+K, A[i+1]-K)) return res
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: if not A: return 0 A.sort() x = A[0] y = A[-1] res = y - x for i in range(len(A)): A[i] += 2 * K x = min(A[0], A[(i+1)%len(A)]) y = max(A[i], y) res = min(res, y - x) return res
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() if len(A) == 1:return 0 res = A[-1] - A[0] for i in range(len(A)-1): this_max = max(A[i]+K, A[-1]-K) this_min = min(A[0]+K, A[i+1]-K) res = min(res, this_max - this_min) return res
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() minn,maxx = A[0],A[-1] ans = maxx - minn for i in range(len(A)-1): a,b = A[i], A[i+1] ans = min(ans, max(maxx-K,a+K) - min(minn+K,b-K)) return ans
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() res = A[-1] - A[0] for i in range(len(A)-1): big = max(A[-1], A[i]+2*K) small = min(A[0]+2*K, A[i+1]) res = min(res, big - small) return res
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
# [1, 3, 6] class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() arr_min, arr_max = A[0], A[-1] ans = arr_max - arr_min for i in range(len(A) - 1): a, b = A[i], A[i + 1] ans = min(ans, max(arr_max - K, a + K) - min(arr_min + K, b - K)) return ans
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() if A[-1]-A[0]<=K: return A[-1]-A[0] else: res = A[-1]-A[0] for i in range(len(A)-1): res = min(res, max(A[-1]-K, A[i]+K)-min(A[0]+K, A[i+1]-K)) return res
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], k: int) -> int: A.sort() n=len(A) res=A[n-1]-A[0] for i in range(n-1): mn=min(A[i+1]-k,A[0]+k) mx=max(A[i]+k,A[n-1]-k) res=min(res,mx-mn) return res
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() res = A[-1] - A[0] for i in range(len(A) - 1): big = max(A[-1], A[i] + 2 * K) small = min(A[i + 1], A[0] + 2 * K) res = min(res, big - small) return res
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() v_min, v_max = A[0], A[-1] res = v_max - v_min for i in range(len(A)-1): res = min(res, max(v_max-K, A[i]+K) - min(v_min+K, A[i+1]-K)) return res
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() mi, ma = A[0], A[-1] ans = ma - mi for i in range(len(A) - 1): a, b = A[i], A[i+1] ans = min(ans, max(ma-K, a+K) - min(mi+K, b-K)) return ans
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: if K == 0: return max(A)-min(A) if len(A) == 1: return 0 if max(A) - min(A) <= K: return max(A) - min(A) else: A = sorted(A) dp = [A[-1] - A[0]] n1, n2 = A[-1] - K, A[0] + K for i in range(len(A)-1): dp += [max(A[i]+K, n1) - min(n2, A[i+1]-K)] return min(dp)
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() min_diff=A[-1]-A[0] for i in range(len(A)-1): v_min=min(A[0]+K,A[i+1]-K) v_max=max(A[i]+K,A[-1]-K) min_diff=min(min_diff,v_max-v_min) return min_diff
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add x to A[i] (only once). After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.   Example 1: Input: A = [1], K = 0 Output: 0 Explanation: B = [1] Example 2: Input: A = [0,10], K = 2 Output: 6 Explanation: B = [2,8] Example 3: Input: A = [1,3,6], K = 3 Output: 3 Explanation: B = [4,6,3]   Note: 1 <= A.length <= 10000 0 <= A[i] <= 10000 0 <= K <= 10000
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: A.sort() mi, ma = A[0], A[-1] # Note: using K will only possibly reduce the current difference ans = ma - mi # the modified result will not be greater than the current worsest case (max(A)-min(A)) for i in range(len(A) - 1): a, b = A[i], A[i+1] ans = min(ans, max(ma-K, a+K) - min(mi+K, b-K)) return ans