Advertisement
mynk_shrma

Untitled

Jul 27th, 2023
1,548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.79 KB | None | 0 0
  1. func checkLock(deadLocker []string, a string) bool {
  2.     for _, s := range deadLocker {
  3.         if a == s {
  4.             return false
  5.         }
  6.     }
  7.     return true
  8. }
  9.  
  10. func Solution(startState string, endState string, N int, deadLocker []string) int {
  11.     count := 0
  12.     flag := true
  13.  
  14.     for i := 0; i < len(startState); i++ {
  15.         diff := int(endState[i]) - int(startState[i])
  16.         if diff != 0 {
  17.             for diff != 0 {
  18.                 if diff > 0 {
  19.                     diff--
  20.                     startState = startState[:i] + string(startState[i]+1) + startState[i+1:]
  21.                 } else {
  22.                     diff++
  23.                     startState = startState[:i] + string(startState[i]-1) + startState[i+1:]
  24.                 }
  25.                 if checkLock(deadLocker, startState) {
  26.                     count++
  27.                 } else {
  28.                     flag = false
  29.                     break
  30.                 }
  31.             }
  32.         }
  33.     }
  34.  
  35.     if flag {
  36.         return count
  37.     } else {
  38.         return -1
  39.     }
  40.  
  41. }
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement